Answer a question

I'm submitting form encoded data to a Flask route. If I look at the data directly with request.get_data(), it has the raw form encoded data. However, request.form is always empty, regardless of if I call get_data() or not. Why isn't the data parsed into request.form?

print(request.get_data())
b'reg_no=00PAS013&D2LL=True&DOCCOM_=chiave+%2B+CDC+nell%27armadio+5+25%2F06%2F2019&user_key_urlsafe=agtiY2Etb3BzLXVhdHIxCxIDQkNBIgNvcHMMCxIHQ29tcGFueRiAgICAgICACgwLEgRVc2VyGICAgICA5JEKDA&password=DUMMY%E2%82%ACuro&task_submitted_datetime=2020-01-31T17%3A31%3A43.573414' 
print(request.form.to_dict())
{}

Answers

I found out the issue: you have to always set appropriate headers if you want any of request.form or request.json populated.

To me setting the header Content-Type: application/x-www-form-urlencoded did request.form to work.

Set Content-Type: application/json; charset=utf-8 to get request.json populated.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐