I am having difficulty finding the python request equivalent to the curl command which send the data in the body as JSON.
The curl command looks like
curl -X POST -d '{"key1": "value1", "key2": "value2"}' http://localhost:8080/myapi
With the CURL request, Server(python) gets the data in requset.form like below
ImmutableMultiDict([('{"key1": "value1", "key2": "value2"}', '')])
But when sending the same post request like using request module of python
response = requests.post("http://localhost:8080/myapi", data=json.dumps({
"key1": "value1", "key2": "value2"))
From the server-side, I see the content like
ImmutableMultiDict([('key1', 'value1'), ('key2', 'value2')]) and its type is : <class 'werkzeug.datastructures.ImmutableMultiDict
I am a bit confused about why the request using requests.post having different content at server side compared to using curl request, can someone please help to understand and possible solutions
The issue has solved by adding 'Content-Type:application/json' in both curl and the request made from the request module. Also, I used to check the data in request.form but realized that it is working when I use the request.data, new to python so have missed this, thank you.
Have you tried just sending the dictionary instead of stringified json?
r = requests.post('https://httpbin.org/post', data = {"key1": "value1", "key2": "value2"})
running this through the httpbin test server and running your curl statement yield identical responses (with the exception of the user agent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.