Accessing Payload Data from a RESTful Call

212 views
Skip to first unread message

Welshy

unread,
Feb 5, 2012, 6:45:08 AM2/5/12
to Tornado Web Server
Hi,

I have an app set up using Backbone.js and Tornado. One of the
Backbone models is making a POST request to a handler (when saving)
and passing along Payload Data;


Request URL:http://localhost:8888/message
Request Method:POST
Status Code:200 OK
Request Headers
POST /message HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Content-Length: 68
Cache-Control: no-cache
Origin: http://localhost:8888
X-Requested-With: XMLHttpRequest
Pragma: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/
535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7
Content-Type: application/json
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:8888/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: msg_error=SW5jb3JyZWN0IHBhc3N3b3Jk|1328223024|
f6b00f25d745537771171528df659d5ab6752def; user_id="Mg==|1328223162|
5d71ccc88136f86ec5434957515189bc80850cd9"; username="a2V2aW4=|
1328223162|cda11d83c9269fbb8e6f74c88245432a46262630"
Request Payload
{"text":"foobar","date":"2012-02-05T11:23:46.105Z","author":"Kevin"}
Response Headers
HTTP/1.1 200 OK
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Server: TornadoServer/2.2
AllDocumentsStylesheetsImagesScriptsXHRFontsWebSocketsOther


I'm trying to access the variables text, date and author from the
Request Payload section. the get_argument method of the Request
Handler returns empty values when trying to access anything from the
payload e.g. self.get_argument('text', None) // => None

How can I access the Request Payload?

Aleksandar Radulovic

unread,
Feb 5, 2012, 8:30:21 AM2/5/12
to python-...@googlegroups.com
Hi,

Data you're POSTing is not url-encoded data (usual way to post) and
get_argument() works only with data posted like that.
What you need to do is to load the JSON encoded data. In your handler,
you can do something like this:

data = json.loads(self.request.body)

Variable data is now a Python dict() which you can inspect, i.e.
data.get('text') would be 'foobar'.

Regards,
alex

--
a lex 13 x
http://a13x.net | @a13xnet

Reply all
Reply to author
Forward
0 new messages