目录
如何知道接口的传参类型常见的传参类型及参数传入方式content-type: application/jsoncontent-type: application/x-www-form-urlencoded没有content-type,请求参数放在url中,如图content-type: multipart/form-datacontent-type: text/plain
如何知道接口的传参类型
浏览器》F12》查看接口请求头》content-type ![在这里插入图片描述](https://img-blog.csdnimg.cn/83c5eaacb78d4df68685838a06396098.png)
常见的传参类型及参数传入方式
content-type: application/json
请求内容写为字典然后使用request方法json参数传入 实例 ![在这里插入图片描述](https://img-blog.csdnimg.cn/e66553cbe3ac453db95e3ffe5b922422.png)
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()转成字符串就可以了 可以参考浏览器解析后的内容 ![在这里插入图片描述](https://img-blog.csdnimg.cn/f3c85146d3b34633b84cb341930ac8cc.png)
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参数传入 实例 ![在这里插入图片描述](https://img-blog.csdnimg.cn/9670bff74c0a44cf87eabff36daba6bd.png)
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
![在这里插入图片描述](https://img-blog.csdnimg.cn/ab9710ced3e6444f84fffc861d1514c8.png)
请求内容写为字典然后使用request方法files参数传入 **实例 请求参数原始格式
请求参数浏览器解析后格式 参数传入实例 ![在这里插入图片描述](https://img-blog.csdnimg.cn/d304d36bb2584ff5ba2f44751a6608fe.png![在这里插入图片描述](https://img-blog.csdnimg.cn/e2f5f17b5c6f4c31827d89553ad58f0d.png)
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)
|