Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Falcon - get POST data

I try to use falcon package in my project. Problem is I didn't find a way to get body data from the HTTP post request.

I used code from example, but req.stream.read() doesn't return JSON as expected.

The code is:

raw_json = req.stream.read()
result.json(raw_json, encoding='utf-8')
resp.body = json.dumps(result_json, encoding='utf-8')

How to get the POST data?

Thanks for any help

like image 793
Gomi Avatar asked Feb 08 '18 06:02

Gomi


4 Answers

in falcon 2, if you work with json type, use req.media

for example:

import falcon
from json import dumps

class Resource(object):
    def on_post(self, req, resp, **kwargs):
        result = req.media
        # do your job
        resp.body = dumps(result)


api = falcon.API()

api.add_route('/test', Resource())
like image 196
mosi_kha Avatar answered Nov 13 '22 23:11

mosi_kha


Little digging into the problem led to the following linked issue on github. It states that falcon framework at least in its version 0.3 and working with Python 2 didn't parse data 'POSTed' as string if they are aptly escaped. We could use more information on what data you are trying to send over POST request and in what format is that being sent, as in if its being send as simple text, or with Header Information Content-Type:application/json, or if its coming through an HTML form.

While the exact issue is not clear from the question I could still suggest trying to use bounded_stream instead of stream as in:

raw_json = req.bounded_stream.read()
result.json(raw_json, encoding='utf-8')
resp.body = json.dumps(result_json, encoding='utf-8')

for the official documentation suggests use of bounded_stream where uncertain conditions such as Content-Length undefined or 0, or if header information is missing altogether.

bounded_stream is described as the following in the official falcon documentation.

File-like wrapper around stream to normalize certain differences between the native input objects employed by different WSGI servers. In particular, bounded_stream is aware of the expected Content-Length of the body, and will never block on out-of-bounds reads, assuming the client does not stall while transmitting the data to the server.

Falcon receives the HTTP requests data as buffer object as passed by WSGI wrapper which receives the data from client, and its possible it doesn't run proper parsing on top of the data to convert to a more usable data structure for performance reasons.

like image 30
Shubham Mishra Avatar answered Nov 13 '22 23:11

Shubham Mishra


Big thanks to Ryan (and Prateek Jain) for the answer.

The solution is simply to put app.req_options.auto_parse_form_urlencoded=True. For example:

import falcon

class ThingsResource(object):
    def on_post(self, req, resp):
        value = req.get_param("value", required=True)
        #do something with value

app = falcon.API()
app.req_options.auto_parse_form_urlencoded=True

things = ThingsResource()

app.add_route('/things', things)
like image 6
Zezombye Avatar answered Nov 13 '22 23:11

Zezombye


The field you're looking for is somewhat confusingly named, but it's req.media:

Returns a deserialized form of the request stream. When called, it will attempt to deserialize the request stream using the Content-Type header as well as the media-type handlers configured via falcon.RequestOptions.

If the request is JSON, req.media already contains a python dict.

like image 3
mb21 Avatar answered Nov 13 '22 22:11

mb21