More than one field on POST with ReST

41 views
Skip to first unread message

Carolina Nogueira

unread,
Mar 31, 2014, 9:04:52 AM3/31/14
to web...@googlegroups.com
Hi guys!

I was trying to execute a POST using ReST inside a function. This post has two fields. When I try to do it with a curl, it works fine, but when I do exactly the same POST inside a function, it gives me an error.

On my model:
db.define_table('algorithm',
                Field('name', 'string',length=255,unique=True),
                Field('description', 'text'),
                format='%(name)s')

And on my controller:

import requests
from requests.auth import HTTPBasicAuth

@request.restful()
def api():
    response.view = 'generic.'+request.extension

    def GET(*args,**vars):
        patterns = 'auto'
        parser = db.parse_as_rest(patterns,args,vars)
        if parser.status == 200:
            return dict(content=parser.response)
        else:
            raise HTTP(parser.status,parser.error)

    def POST(table_name,**vars):
        return db[table_name].validate_and_insert(**vars)

    def PUT(table_name,record_id,**vars):
        return db(db[table_name]._id==record_id).update(**vars)

    def DELETE(table_name,record_id):
        return db(db[table_name]._id==record_id).delete()
    return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)

def add_algorithm():
    """
    form to add new algorithms on database.
    """
    alg_form=SQLFORM.factory(Field('name', requires=IS_NOT_IN_DB(db,db.algorithm.name)),
                             Field('description'),
                             table_name='algorithm')

    if alg_form.accepts(request,session):
        payload = {'name':alg_form.vars.name, 'description':alg_form.vars.description}
        r = request.post("http://127.0.0.1:8000/webpricer/default/api/algorithm.json", data=payload, auth=('user', 'pass'))
        response.flash = payload
    elif alg_form.errors:
        response.flash = T('Please check for errors on form!')
    else:
        response.flash = T('Please fill the form')
    return dict(alg_form=alg_form)

When I click to submit the values, I receive the following error:
<type 'exceptions.TypeError'>('NoneType' object is not callable)

The variables are as follow:
global request     <Storage {'_vars': None, 'function': 'add_algori...ueira/Downloads/web2py/applications/webpricer/'}>
request.post     None
global data     <function data>
global auth     <gluon.tools.Auth object>
r     undefined
payload     {'description': 'Description', 'name': 'webpricer'}

For sure I'm doing something dumb, but I'm stuck on this error some days and can't found what I'm doing wrong... Didn't find any related error on past topics.





Massimo Di Pierro

unread,
Mar 31, 2014, 9:40:08 AM3/31/14
to web...@googlegroups.com
I need the complete traceback but I think the error is here:

 r = request.post("http://127.0.0.1:8000/webpricer/default/api/algorithm.json", data=payload, auth=('user', 'pass'))

there is no request.post, unless you imported something that overrides the default request object and you should not to that. Web2py replays on the existence of the global request object.
Reply all
Reply to author
Forward
0 new messages