简体   繁体   English

Python Bottle如何将参数作为json传递

[英]Python Bottle how to pass parameter as json

I created an api for openerp using bottle 我用瓶子为openerp创建了一个api

It works well while access using browser 使用浏览器访问时效果很好

I don't know how to pass it as json parameters 我不知道如何将其作为json参数传递

The Problem is 问题是

how can i call using api and pass json parameters like 我如何使用api调用并传递json参数

http://localhost/api?name=admin&password=admin&submit=Submit

Here is my wsgi code app.wsgi 这是我的wsgi代码app.wsgi

import json
import os
import sys
import bottle
from bottle import get, post, run,request,error,route,template,validate,debug
def login():
        import xmlrpclib
        username = request.forms.get('name')
        pwd = request.forms.get('password')
        dbname = 'more'
        sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
        uid = sock_common.login(dbname, username, pwd)
        if uid:
                return json.dumps({'Success' : 'Login Sucessful'])

def index():
        return '''
        <html>
        <head>
                <title> Portal</title>
        </head>
        <body>Welcome To PORTAL
   <form method="GET" action="/api/links" enctype="multipart/form-data">
   Name:<input name="name" type="text"/><br>
   Password:<input name="password" type="password"/><br>
   <input type="submit" value="Submit" name="submit">
   </form>
   </body>
   </html>'''

def links():
        return '''
        <html>
        <head>
                <title> Portal</title>
        </head>
        <body>
    <a href="/api/advisor">Advisor<br>
   </body>
   </html>'''

application = bottle.default_app()
application.route('/', method="GET", callback=index)
application.route('/', method="POST",callback=login)

request.forms is used for POST or PUT requests. request.forms用于POST或PUT请求。 The form in your code uses GET, not POST, so you should use request.query.getall , which gives you access to "URL arguments". 代码中的表单使用GET而不是POST,因此您应该使用request.query.getall ,它允许您访问“URL参数”。

I don't see anything wrong with the code (except pep8 changes), only problem I see is method of the form and location, see the fixed version below ... 我没有看到代码有什么问题(pep8更改除外),我看到的唯一问题是表单和位置的方法,请参阅下面的固定版本...

import json
import os
import sys
import bottle
from bottle import get, post, run, validate, request, error, route, template, debug


def login():
    import xmlrpclib
    username = request.forms.get('name')
    pwd = request.forms.get('password')
    dbname = 'more'
    sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
    uid = sock_common.login(dbname, username, pwd)
    if uid:
        return json.dumps({'Success': 'Login Sucessful'})


def index():
        return '''
        <html>
        <head>
                <title> Portal</title>
        </head>
        <body>Welcome To PORTAL
   <form method="POST" action="/" enctype="multipart/form-data">
   Name:<input name="name" type="text"/><br>
   Password:<input name="password" type="password"/><br>
   <input type="submit" value="Submit" name="submit">
   </form>
   </body>
   </html>'''


def links():
        return '''
        <html>
        <head>
                <title> Portal</title>
        </head>
        <body>
    <a href="/api/advisor">Advisor<br>
   </body>
   </html>'''


application = bottle.default_app()
application.route('/', method="GET", callback=index)
application.route('/', method="POST", callback=login)

application.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM