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

后端 未结 3 591
时光说笑
时光说笑 2020-12-19 10:46

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

>>> req = requests.Request(\'PO         


        
3条回答
  •  长情又很酷
    2020-12-19 11:22

    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.

提交回复
热议问题