Why can't I send `None` as data in a POST request using Python's `requests` library?

瘦欲@ 提交于 2019-12-01 03:24:13

问题


It seems that when a key in data has a value of None, the key isn't included by requests.

>>> req = requests.Request('POST', 'http://google.com', data=dict(a=None, b=1))
>>> req.prepare().body
'b=1'

Why is this the case? I was expecting an empty string, or something like json.dumps(d) where None is rendered as null. I'm sure there's a good reason -- just curious about what it is. (One thing I can think of is that maybe there just isn't an encoding of null or None available to the POST request -- is that true?)

Additionally curious -- why does requests silently ignore such data instead of throwing an error?


回答1:


Setting a dictionary element to None is how you explicitly say that you don't want that parameter to be sent to the server.

I can't find this mentioned specifically in the requests.Request() documentation, but in Passing Parameters in URLs it says:

Note that any dictionary key whose value is None will not be added to the URL's query string.

Obviously it uses consistent logic for POST requests as well.

If you want to send an empty string, set the dictionary element to an empty string rather than None.




回答2:


I had similar issue with a blank value and this was my workaround. I sent the data as json string and set content type headers as application/json. This seems to send the whole data across as expected. Took quite some time to figure out. Hope this helps someone.

import requests
import json

header = {"Content-Type":"application/json"}

data = {
    "xxx": None,
    "yyy": "http://",
    "zzz": 12345
    }

res = requests.post('https://httpbin.org/post', 
                    data=json.dumps(data), headers=header)

obj = json.loads(res.text)

print obj['json']



回答3:


I had the same question few days ago and if you replace data with json it should work for you, as now None will be sent in the body.

request('POST', 'http://google.com', json=dict(a=None, b=1))


来源:https://stackoverflow.com/questions/36271697/why-cant-i-send-none-as-data-in-a-post-request-using-pythons-requests-libr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!