接口自动化常见的几种传参类型及参数传递方式

本文介绍了如何确定接口的传参类型,并列举了常见的传参类型及其使用方式,包括application/json、application/x-www-form-urlencoded等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何知道接口的传参类型

浏览器》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参数传入
**实例
请求参数原始格式


请求参数浏览器解析后格式
在这里插入图片描述
参数传入实例
![在这里插入图片描述](https://img-blog.csdnimg.cn/d304d36bb2584ff5ba2f44751a6608fe.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)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

要站在顶端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值