目录
- 如何知道接口的传参类型
- 常见的传参类型及参数传入方式
- content-type: application/json
- content-type: application/x-www-form-urlencoded
- 没有content-type,请求参数放在url中,如图
- content-type: multipart/form-data
- content-type: text/plain
如何知道接口的传参类型
浏览器》F12》查看接口请求头》content-type
常见的传参类型及参数传入方式
content-type: application/json
请求内容写为字典然后使用request方法json参数传入
实例
url = 127.0.0.1/testdemo
paylod = {
"dataId":"4735355018682293348",
"formId":"2375346656471163011",
"layoutId":"2375346656472563013",
"module":"customer"
}
headers = {
"Content-Type": "application/json",
"Cookie": "Cookie"
}
response = request("POST", url, headers=headers, json=payload)
content-type: application/x-www-form-urlencoded
请求内容写为字典然后使用request方法data参数传入
实例
这里我们依然将请求体的参数写为字典,个人感觉字典的可读性强于字符串拼接
url = 127.0.0.1/testdemo
paylod = {
"customerId": "4735354895615677672",
"key": "saleChances_permision"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "Cookie"
}
# 这里需要注意 使用data参数传入即可,无需再对payload做字典转字符串的操作
response = request("POST", url, headers=headers, data=payload)
如果传入参数中,字典的键的值是一个字符串,如下图
queryStr是一个很长的字符串,我们一样可以把它写成字典,然后使用json.dumps()转成字符串就可以了
可以参考浏览器解析后的内容
url = 127.0.0.1/testdemo
queryStr = { # 先写成字典,可读性更高
"conditions": [],
"customConditions": [],
"typeCustomConditions": [],
"filter": {
"type": "mine",
"targetId": "",
"searchType": ""
},
"openSeaIds": [],
"customColumns": [],
"includePermission": true,
"orderBy": "",
"orderWay": "",
"customType": "",
"pageNo": 1,
"pageSize": 20,
"cacheCountSqlKey": "customer_ct_6045346601948646126_1653552711411",
"cacheSqlKey": "customer_6045346601948646126_1653552711411",
"queryCount": false
}
payload = {
"moduleType": "customer",
"type": "mine",
"queryStr": json.dumps(queryStr ), # 转成字符串传入
"employeeId": "6045346601948646126"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "Cookie"
}
response = request("POST", url, headers=headers, data=payload)
没有content-type,请求参数放在url中,如图
请求内容写为字典然后使用request方法params参数传入
实例
url = 127.0.0.1/testdemo
paylod = {
"customerId": "4735354895615677672",
"queryType": ""
}
headers = {
"Cookie": "Cookie"
}
# 这里需要注意 使用params参数传入即可,无需再对payload做字典转字符串的操作
response = request("POST", url, headers=headers, params=payload)
content-type: multipart/form-data
请求内容写为字典然后使用request方法files参数传入
**实例
请求参数原始格式
请求参数浏览器解析后格式
参数传入实例
content-type: text/plain
跟application/x-www-form-urlencoded一样
请求内容写为字典然后使用request方法data参数传入
实例
url = 127.0.0.1/testdemo
paylod = {
"customerId": "4735354895615677672",
"key": "saleChances_permision"
}
headers = {
"Content-Type": "text/plain",
"Cookie": "Cookie"
}
# 这里需要注意 使用data参数传入即可,无需再对payload做字典转字符串的操作
response = request("POST", url, headers=headers, data=payload)