- 一、如何发送post请求
- 二、post的参数如何传入
1、需要测试的接口url
2、接口的请求参数(header、 param)
代码如下(示例):
import requests
url = "http://v.juhe.cn/exp/index"
header = {
"content-type”:"application/json"
}
param = {
"key":"5c14a269******81ee388a"
"com":888,
"no":8928288282111
}
resp = requests.post(url,headers=header,json=param)
print(resp.text)
二、post的参数如何传入
一般情况下,使用request传参时,会传入data或者json
那什么时候传data,什么时候传json?
1、根据接口请求头header去判断,如果content-type为application/json,为json格式,则使用json参数
方法一:
直接传json
代码如下(示例):
import requests
url = "http://v.juhe.cn/exp/index"
header = {
"content-type”:"application/json"
}
param = {
"key":"5c14a269******81ee388a"
"com":888,
"no":8928288282111
}
resp = requests.post(url,headers=header,json=param)
print(resp.text)
方法二:
先将参数转成json字符串,直接传data
代码如下(示例):
import requests
import json
url = "http://v.juhe.cn/exp/index"
header = {
"content-type”:"application/json"
}
param = {
"key":"5c14a269******81ee388a"
"com":888,
"no":8928288282111
}
#将字典转成json串
params = json.dumps(param)
resp = requests.post(url,headers=header,data=params)
print(resp.text)
2、如果content-type为application/x-www-form-urlencoded,为表单格式,则使用data参数
代码如下(示例):
import requests
url = "http://v.juhe.cn/exp/index"
header = {
"content-type”:"application/x-www-form-urlencoded"
}
param = {
"key":"5c14a269******81ee388a"
"com":888,
"no":8928288282111
}
resp = requests.post(url,headers=header,data=param)
print(resp.text)



