Accessing POST JSON and a requests raw body
sshmendez opened this issue · 4 comments
As you can tell, I am trying to access JSON sent as the body of a POST request. With a function like this I have access to the Request
object
def func(request):
print(dict(request.values))
This code will print data received as x-www-form-urlencoded
i.e. structured like this foo=0&bar=2
however if the data were structured as JSON
, request.values
is suddenly empty. Any chance I can create JSON centered API's in clastic?
Sure. Clastic is built on Werkzeug, so Request and Response are mostly those Werkzeug wrappers. Try .get_json()
instead of .values
: https://werkzeug.palletsprojects.com/en/1.0.x/wrappers/#json
I found that after looking into the code some more. I guess I'm a noob in python backend frameworks, Werkzeug seems to be everywhere
edit:
I went the .data()
route and parsed it to json myself. Doesn't look like I have access to .get_json()
for whatever reason
Yeah werkzeug's basically doing that behind the scenes anyways, not a huge convenience delta. Might be a recent addition to werkzeug, even the most recent published version of clastic is based on a werkzeug version that's at least a few months old.
re: noob-ness, no time like the present when it comes to learning! Be sure to check out the new tutorials on the in-progress docs: https://python-clastic.readthedocs.io/en/latest/