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
  • None is a special class (python3.x) / type (python2.x) that's intentionally designed to denote the absence of a value. If you want an empty string, make a an empty string (a=""). More info here Commented Mar 28, 2016 at 21:54

4 Answers 4

16

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response! What I'm wondering now is why requests provides this facility for ignoring parameters. If I don't want it to be sent, can't I just not include it in the dict?
@EliRose this is provided because you might have defaults set on a session that you don't want sent. Since the flow you're using ignores that, you can always just manually update the body to be what you expect.
@sigmavirus24: Interesting -- could you possibly point me to an example of the use case you're talking about? re: the alternative you mention; in real life I'm just using request.post(...), the req.prepare().body is just for illustration purposes.
@EliRose They probably all use a common routine to process the data dictionary, and None is the placeholder used to remove parameters.
6

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']

Comments

4

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))

Comments

0

The best way is to use parameter json instead of data. If you use :

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

It works well

2 Comments

Did you learn how to delete an answer post of yours by now? I note that the one you reported having problems with deleting had to be deleted by others. Do you still need help with finding out how you can delete yourself?
herve-guerin, I checked all your currently visible posts. None of them has any indicator that you know how to edit, in addition to your recent statement that you perceive deleting as impossible for you. Let me help you if you really have a problem. Or try to click the word "Edit" or the word "Delete" under one of your posts and confirm that it allows you to, it does not immediatly delete and you can cancel an edit. I just want to make sure that you have all the intended tools for making valuable contributions here at your disposal that you are meant to have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.