0

我使用瓶子为 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)
4

2 回答 2

2

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

于 2013-06-11T12:43:18.547 回答
0

我没有看到代码有任何问题(除了 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()
于 2014-09-24T21:26:56.070 回答