Thanks for your help, I got the GET and POST methods working.
PUT for updating and DELETE don't seem to work though.
I changed my API on your suggestion to wrap dict() around the DAL calls:
@request.restful()
def secapi():
response.view = 'generic.'+request.extension
print "args ",request.args
print "vars ",request.vars
#pprint.pprint(request, indent=4)
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 dict(db[table_name].validate_and_insert(**vars))
def PUT(table_name,record_id,**vars):
return dict(db(db[table_name]._id==record_id).update(**vars))
def DELETE(table_name,record_id):
return dict(db(db[table_name]._id==record_id).delete())
return dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)
But, I see this:
curl -X GET http://localhost:8007/apitest/default/secapi/secrets/id/21.json
{"content": [{"datavalue": "othertest", "id": 21}]}
Can do my get, then I try:
curl -X PUT -d "datavalue=updated" http:
//localhost:8007/apitest/default/secapi/secrets/id/21
invalid arguments
I see "invalid arguments" and the row is not updated:
curl -X GET http://localhost:8007/apitest/default/secapi/secrets/id/21.json
{"content": [{"datavalue": "othertest", "id": 21}]}
Is my use of curl incorrect, or is the api function for PUT coded
incorrectly?
Sorry if it's a simple thing
Thanks
On Monday, 9 November 2015 17:34:27 UTC, Anthony wrote:
>
> The book is incorrect. If you want to rely on the generic.json view to
> generate the JSON response, then you must return a dictionary (otherwise,
> no view will be called, and whatever you return will be returned directly).
> In this case, the .validate_and_insert method returns a DAL Row object (not
> a database record, just a Row object that contains "error" and "id" keys).
> Apparently, when you return a Row object, the response body ends up being
> just a concatenation of its keys, so in this case, we get "errorid" as the
> response.
>
> You have a few options:
>
> 1. Put the result in a dictionary:
>
> return dict(result=db[table_name].validate_and_insert(**vars))
>
> 2. Convert the Row object itself to a dictionary:
>
> return db[table_name].validate_and_insert(**vars).as_dict()
>
> 3. Return JSON directly:
>
> from gluon.serializers import json
> return json(db[table_name].validate_and_insert(**vars))
>
> Anthony
>
> On Monday, November 9, 2015 at 10:50:21 AM UTC-5, Gary Cowell wrote:
>>
>> Lookint at providing simple REST API to database tables through web2py. I
>> used this example from the book:
>> @request.restful()
>> def secapi():
>> 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)
>>
>>
>> Now the GET methods work, I can get json responses with curl:
>>
>> curl http://localhost:8007/apitest/default/secapi/secrets.json
>> {"content": [{"datavalue": "foobar", "id": 1}, ... ]}
>>
>>
>> When I POST though:
>>
>> $ curl --data "datavalue=sometest" http:
>> //localhost:8007/apitest/default/secapi/secrets.json
>>
>> I get the response:
>>
>> errorsid
>>
>> When I really want the resource URL of the created thing.
>>
>> It did actually create the row:
>>
>> curl http://localhost:8007/apitest/default/secapi/secrets/id/20.json
>> {"content": [{"datavalue": "sometest", "id": 20}]}
>>
>> So my question is, how do I code the POST such that it returns the
>> resource URL that was created?
>>
>> Thanks
>>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.