Python Falcon - get POST data Python Falcon - get POST data python python

Python Falcon - get POST data


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.


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

for example:

import falconfrom json import dumpsclass 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())


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 falconclass ThingsResource(object):    def on_post(self, req, resp):        value = req.get_param("value", required=True)        #do something with valueapp = falcon.API()app.req_options.auto_parse_form_urlencoded=Truethings = ThingsResource()app.add_route('/things', things)