【问题标题】:Python Bottle how to pass parameter as jsonPython Bottle如何将参数作为json传递
【发布时间】:2014-09-25 05:26:56
【问题描述】:

我使用瓶子为 openerp 创建了一个 api

使用浏览器访问时效果很好

我不知道如何将它作为json参数传递

问题是

如何使用 api 调用并传递 json 参数,例如

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

这是我的 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)

【问题讨论】:

    标签: python json wsgi bottle


    【解决方案1】:

    request.forms 用于 POST 或 PUT 请求。您的代码中的表单使用 GET,而不是 POST,因此您应该使用 request.query.getall,它可以让您访问“URL 参数”。

    【讨论】:

    • 我不知道如何以及在哪里使用 query.getall 请您在这里发布一个示例
    【解决方案2】:

    我没有看到代码有任何问题(除了 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多