1.背景
某个项目中的一个查询接口,为了前端数据处理方便,get请求也传递了多层嵌套的参数。如下格式
{
"page_num": 1,
"page_size": 10,
"filter": {
"type": 2,
"status": 1,
"sort": {
"product": 1,
"time": -1
}
}
}
2.问题
使用requests内置的方法,将字典直接赋值给params参数发送请求,发现请求的结果和预期不符,没有对filter字段进行urlencode, 请求的filter字段被requests默认处理为数组格式
In [1]: import requests
In [2]: url = 'http://httpbin.org/get'
In [3]: query = {
...: "page_num": 1,
...: "page_size": 10,
...: "filter": {
...: "type": 2,
...: "status": 1,
...: "sort": {
...: "product": 1,
...: "time": -1
...: }
...: }
...: }
In [4]: resp = requests.get(url, params=query)
In [5]: resp.json()
Out[5]:
{'headers': {'Accept-Encoding': 'gzip, deflate, br',
'X-Amzn-Trace-Id': 'Root=1-6264b4fa-530ceb906501223b7119a440',
'Accept': '*/*',
'X-Proxyman-Scripting': 'Handled by Script: Name=Untitled. Rule=httpbin.org/*',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.27.1',
'Content-Length': '0'},
'args': {'filter': ['type', 'status', 'sort'], # 👈 filter参数被处理为数组
'page_num': '1',
'page_size': '10'},
'origin': '94.74.125.61',
'url': 'http://httpbin.org/get?page_num=1&page_size=10&filter=type&filter=status&filter=sort'}
In [6]: resp.request.url
Out[6]: 'http://httpbin.org/get?page_num=1&page_size=10&filter=type&filter=status&filter=sort'。# 👈 实际请求的filter参数
3. 解决方式
自己对filter字段进行urlencode然后发送.? Python3中内置函数为?
urllib.parse.quote?
urllib.parse.unquote
In [1]: import requests
In [2]: url = 'http://httpbin.org/get'
In [3]: query = {
...: "page_num": 1,
...: "page_size": 10
...: }
In [4]: filter = { # 👈 将filter参数拆出来单独处理
...: "type": 2,
...: "status": 1,
...: "sort": {
...: "product": 1,
...: "time": -1
...: }
...: }
In [5]: import urllib
# 👇 将filter参数进行urlencode手动拼接到url中
In [6]: url += '?filter=' + urllib.parse.quote(str(filter))
# 👇 其余参数依然使用params传参
In [7]: resp = requests.get(url, params=query)
Out[7]:
{'headers': {'Accept-Encoding': 'gzip, deflate, br',
'X-Amzn-Trace-Id': 'Root=1-6264b6e8-397fd01435a1d24222e4820a',
'Accept': '*/*',
'X-Proxyman-Scripting': 'Handled by Script: Name=Untitled. Rule=httpbin.org/*',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.27.1',
'Content-Length': '0'},
'args': {'filter': "{'type': 2, 'status': 1, 'sort': {'product': 1, 'time': -1}}", # 👈 args参数解析正确
'page_num': '1',
'page_size': '10'},
'url': "http://httpbin.org/get?filter={'type'%3A 2%2C 'status'%3A 1%2C 'sort'%3A {'product'%3A 1%2C 'time'%3A -1}}&page_num=1&page_size=10",
'origin': '94.74.125.61'}
In [8]: resp.json()
# 👇 url数据处理正确
In [9]: resp.request.url
Out[9]: 'http://httpbin.org/get?filter=%7B%27type%27%3A%202%2C%20%27status%27%3A%201%2C%20%27sort%27%3A%20%7B%27product%27%3A%201%2C%20%27time%27%3A%20-1%7D%7D&page_num=1&page_size=10'
|