Senthilnathan
Senthilnathan

Reputation: 800

Python Bottle how to pass parameter as json

I created an api for openerp using bottle

It works well while access using browser

I don't know how to pass it as json parameters

The Problem is

how can i call using api and pass json parameters like

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

Here is my wsgi code 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)

Upvotes: 0

Views: 1499

Answers (2)

Adeel
Adeel

Reputation: 781

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 ...

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()

Upvotes: 0

David Futcher
David Futcher

Reputation: 320

request.forms is used for POST or PUT requests. The form in your code uses GET, not POST, so you should use request.query.getall, which gives you access to "URL arguments".

Upvotes: 2

Related Questions