Guru Guru - 1 month ago 10
Python Question

POST service parameter - FLASK

This is what i get

b'{"data": "https://files.slack.com/files-pri/T03HPFD2P-F2RU2S4R0/scher7-om-14.52.47.png"}'


when i print my request data from a flask POST web service.

print(request.data)


But when i do
url = request.form.get('data', None)

I get value of
url
variable
None

Why?

Answer

Your problem is that you sent the request as follows:

req = requests.post(url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})

When sending json data, you should format the request in requests with the json parameter as follows:

req = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})

Further, in your flask app, you can access submitted json data via request.json

Because the content type is application/json -- it will not appear in request.form -- Data will appear in request.form when a request is received with an appropriate content type such as multipart/form-data

Comments