I have code within a Flask application that uses JSONs in the request, and I can get the JSON object like so:
JavaScript
x
2
1
Request = request.get_json()
2
This has been working fine, however I am trying to create unit tests using Python’s unittest module and I’m having difficulty finding a way to send a JSON with the request.
JavaScript
1
3
1
response=self.app.post('/test_function',
2
data=json.dumps(dict(foo = 'bar')))
3
This gives me:
JavaScript
1
5
1
>>> request.get_data()
2
'{"foo": "bar"}'
3
>>> request.get_json()
4
None
5
Flask seems to have a JSON argument where you can set json=dict(foo=’bar’) within the post request, but I don’t know how to do that with the unittest module.
Advertisement
Answer
Changing the post to
JavaScript
1
4
1
response=self.app.post('/test_function',
2
data=json.dumps(dict(foo='bar')),
3
content_type='application/json')
4
fixed it.
Thanks to user3012759.