Flask Testing A Put Request With Custom Headers
Im trying to test a PUT request in my Flask app, using flasks test client. Everything looks good to me but i keep getting 400 BAD request. I tried the same request using POSTMAN
Solution 1:
You do need to actually encode the data to JSON:
import json
with app.test_client() as client:
api_response = client.put(api, data=json.dumps(data), headers=headers)
Setting data
to a dictionary treats that as a regular form request, so each key-value pair would be encoded into application/x-www-form-urlencoded
or multipart/form-data
content, if you had used either content type. As it is, your data is entirely ignored instead.
Solution 2:
I think it is simpler to just pass the data using the json
parameter instead of the data
parameter:
reponse = test_client.put(
api,
json=data,
)
Quoting from here:
Passing the json argument in the test client methods sets the request data to the JSON-serialized object and sets the content type to application/json.
Post a Comment for "Flask Testing A Put Request With Custom Headers"