My URL can take null as a value for a query param like so:
https:/api.server.com/v1/users/?parent=null
When I'm trying to send the request in Python using Requests, I'm passing None:
requests.get(
BASE_URL + USER_URL,
headers = get_headers(),
timeout = 60,
params = {
'parent': None
}
)
However, it seems to skip the query param parent if it's set to None. How can I force it?
You cannot pass null values with the requests package as it's the default behavior as stated in the docs:
Note that any dictionary key whose value is None will not be added to the URL’s query string.
Some solutions:
null, or;0/-1, or;parent=false or no-parent=true.'null' as it can be interpreted differently depending on the backend framework you're using. But if that's the case you can do so.None param will be skipped as if such a param is not set.
import requests
r = requests.get('http://localhost:9090/events', params = {
'param1': None,
'param2':'',
'param3':'param_val'
})
print(r.url)
Result : http://localhost:9090/events?param2=¶m3=param_val
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With