[英]Python Requests - send null as query params value
My URL can take null as a value for a query param like so:我的 URL 可以将 null 作为查询参数的值,如下所示:
https:/api.server.com/v1/users/?parent=null
When I'm trying to send the request in Python using Requests, I'm passing None
:当我尝试使用请求在 Python 中发送请求时,我正在传递
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
.但是,如果它设置为
None
,它似乎会跳过查询参数parent
。 How can I force it?我怎么能强迫它?
You cannot pass null
values with the resquests
package as it's the default behavior as stated in the docs:您不能使用
resquests
package 传递null
值,因为它是文档中所述的默认行为:
Note that any dictionary key whose value is None will not be added to the URL's query string. 请注意,任何值为 None 的字典键都不会添加到 URL 的查询字符串中。
Some solutions:一些解决方案:
null
, or;null
,或者;0
/ -1
, or;0
/ -1
,或者;parent=false
or no-parent=true
.parent=false
或no-parent=true
。'null'
as it can be interpreted differently depending on the backend framework you're using.'null'
,因为它可以根据您使用的后端框架进行不同的解释。 But if that's the case you can do so.As @lepsch has mentioned, the requests
library will not add None
values to your query parameters.正如@lepsch 所提到的,
requests
库不会将None
值添加到您的查询参数中。 However, I feel like there is a misunderstanding in the entire question.但是,我觉得整个问题存在误解。
The URL https:/api.server.com/v1/users/?parent=null
does not have the query parameter set to None
. URL
https:/api.server.com/v1/users/?parent=null
没有将查询参数设置为None
。 It is in fact set to 'null'
, which is actually a string with the data "null" inside of it.它实际上设置为
'null'
,这实际上是一个字符串,其中包含数据“null”。 Therefore, if you want to do this, all you have to do is change None
in your request to 'null'
:因此,如果您想这样做,您只需将请求中的
None
更改为'null'
:
requests.get(
BASE_URL + USER_URL,
headers = get_headers(),
timeout = 60,
params = {
'parent': 'null'
}
)
# This will request
# https:/api.server.com/v1/users/?parent=null
However, if you want a true null value, then you should consider either putting an empty string value... :但是,如果您想要一个真正的 null 值,那么您应该考虑放置一个空字符串值...:
...
params = {
'parent': ''
}
...
# This will request
# https:/api.server.com/v1/users/?parent=
... or use the actual null encoded character, as described in this answer : ...或使用实际的 null 编码字符,如本答案所述:
requests.get(
BASE_URL + USER_URL + "?parent=%00", # Must be added directly, as to not encode the null value
headers = get_headers(),
timeout = 60
)
# This will request
# https:/api.server.com/v1/users/?parent=%00
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' }) r = requests.get('http://localhost:9090/events', params = { 'param1': None, 'param2':'', 'param3':'param_val' })
print(r.url)打印(r.url)
Result: http://localhost:9090/events?param2=¶m3=param_val结果:http://localhost:9090/events?param2=¶m3=param_val
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.