0

I cant figure out how to get post data from flask.

Below is my content-type

'Content-Type': 'application/x-www-form-urlencoded'

Here is what I see from flask so I know the data is there. I just cant access it.

request.values -> CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('{"email":"david","password":"FU"}', u'')])])

So...how do I get email and password?

email = request.?????
davidism
  • 121,510
  • 29
  • 395
  • 339
Tampa
  • 75,446
  • 119
  • 278
  • 425

2 Answers2

2

Final solution that worked:

data = request.values.to_dict().keys()[0]
data = json.loads(data)

source

Community
  • 1
  • 1
Celeo
  • 5,583
  • 8
  • 39
  • 41
  • 1
    While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono Nov 24 '15 at 20:40
  • Thank you this work for me. The above solution is perfect if you integrate a third party API and you can't modified. In my case I had to consume a POST request with json format on body with headers = 'application/x-www-form-urlencoded'. – mr antoni Jun 06 '17 at 09:09
  • can this be used for data stream? – Yaroslav Dukal Mar 16 '18 at 02:48
1

You can force Flask to treat the data as JSON using request.get_json() setting the force argument to True

data = request.get_json(force=True)
mr antoni
  • 595
  • 1
  • 6
  • 17