RESTful api and JSON POST data with curl

1,161 views
Skip to first unread message

Brent Zeiben

unread,
Apr 18, 2013, 7:24:03 PM4/18/13
to web...@googlegroups.com
Hi,
I am using a python script to organize some data and try to send it into web2py via the @request.restful() api.  In the python script the text is mainly input from a free form text field.  ( I believe some Unicode characters are in there as well, however I remove everything that is ord 128 and above.) 

My problem arises when the text contains an ampersand (&) and probably anything that isn't allowed in without the form encoding.  I thought python json would escape the character some way but it does not seem to.

I am trying to use json to transfer this data to web2py via the curl command within python.

Python Script

import json
import subprocess


url
= 'http://127.0.0.1:8000/testing/default/api/people.json'
data
= {"name":"My Full Name","biography":"Some simple information\n about me & blah blah blah"}


jsondata
= json.dumps(data)
result
= subprocess.Popen(['curl',
   
'--user','username:password',
   
'-d',
   jsondata
,
   url
], stderr=subprocess.PIPE,stdout=subprocess.PIPE).communicate()[0]





The web2py relevant parts

db.py

db.define_table('people',
   
Field('name','string',length=200,requires=IS_NOT_EMPTY()),
   
Field('biography','text'))


default.py


auth
.settings.allow_basic_login=True
@auth.requires_login()
@request.restful()
def api():
    response
.view = 'generic.' + request.extension
   
def POST(table_name,**vars):
       
if table_name == 'people':
           
return db.people.validate_and_insert(**vars)
       
else:
           
raise HTTP(400)
   
return locals()


So when the python script sends the data web2py complains because of the escaping issue with the & in the data.  I was doing some searching and thought that changing the Content Type in the curl command to application/json would help with this but then web2py has nothing in the vars variable.

Thank you,
Brent

Niphlod

unread,
Apr 19, 2013, 3:52:57 AM4/19/13
to web...@googlegroups.com
uhm... I may be wrong but.... request.restful accepts the args as a normal page, that is a application/x-www-form-urlencoded or a multipart/form-data . with curl would be something like curl -d name=blablabla -d otherparameter=blablabla http://theurl

Brent Zeiben

unread,
Apr 19, 2013, 9:48:35 AM4/19/13
to web...@googlegroups.com
Ok Thanks Niphlod

Thought I could use json to prepare the data, using urllib.urlencode on the dictionary instead.  Didn't have to change the curl command.

Thanks again.

Brent

Michele Comitini

unread,
Apr 21, 2013, 4:05:03 PM4/21/13
to web...@googlegroups.com
Brent,

you can read the raw body of a POST using the variable request.body

mic


2013/4/19 Brent Zeiben <bze...@tuheadz.com>

--
 
---
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 web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages