python - Flask: Cannot Decode Incoming JSON Submitted Using Requests -


i'm having issue trying decode python dictionary sent server json. have in application:

payload = {'status':[bathroomid,item,percentage,timestamp]} r=requests.post(url,none,json.dumps(payload)) 

here in flask server:

req = request.get_json() print req['status'] 

when try print content of req['status'], seems python won't recognize dictionary , internal server error.

i tried printing req, , none

what missing?

unless set content-type header application/json in request, flask not attempt decode json found in request body.

instead, get_json return none (which you're seeing right now).

so, need set content-type header in request.

fortunately since version 2.4.2 (released year ago), requests has helper argument post json; set proper headers you. use:

requests.post(url, json=payload) 

alternatively (e.g. using requests < 2.4.2), can set header yourself:

requests.post(url, data=json.dumps(payload), headers={"content-type": "application/json"}) 

here relevant code flask:


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -