python接口自动化-传 json 参数

时间:2022-05-16 00:20:56

一、post请求有两种方法传json参数:

  • 1.传json参数(自动转 json )
  • 2.传data参数(需 json 转换)

python接口自动化-传 json 参数

代码参考:

payload = {
"Jodie":"How are you ?",
"python":123,
"requests":True
}
url = "http://httpbin.org/post" #第一种直接传 json 参数(推荐使用这种)
r1 = requests.post(url, json=payload) # json 参数直接自动传 json
print(r1.text) print("---------------------------------------")
#第二种传 data 参数,需要转 json
r2 = requests.post(url, data=json.dumps(payload)) #传 data 参数就需要传 json
print(r2.text)

运行后的打印结果:

F:\test-req-py\venv\Scripts\python.exe F:/test-req-py/day3/t3.py
{
"args": {},
"data": "{\"Jodie\": \"How are you ?\", \"python\": 123, \"requests\": true}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "59",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": {
"Jodie": "How are you ?",
"python": 123,
"requests": true
},
"origin": "223.104.210.143, 223.104.210.143",
"url": "https://httpbin.org/post"
}

---------------------------------------
{
"args": {},
"data": "{\"Jodie\": \"How are you ?\", \"python\": 123, \"requests\": true}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "59",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": {
"Jodie": "How are you ?",
"python": 123,
"requests": true
},
"origin": "223.104.210.143, 223.104.210.143",
"url": "https://httpbin.org/post"
}

Process finished with exit code 0

二、快递查询案例

1.例如打开快递网:http://www.kuaidi.com/,搜索 75135471609813 单号,判断它的状态是不是已签收

python接口自动化-传 json 参数

python接口自动化-传 json 参数

实操中原本的理想代码,实际没不通:

#coding:utf-8
import requests
import json
url = "http://www.kuaidi.com/index-ajaxselectcourierinfo-75135471609813-zhongtong.html"
h = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/70.0.3538.110 Safari/537.36",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Referer":"http://www.kuaidi.com/",
"Cookie":"lang=zh-cn; theme=default; sid=ngr6ji5prpj85cmj7lncnccap7; "
"UM_distinctid=169c4b016739f-069c297f9ff0e7-3a3a5d0c-100200-169c4b016752ec; "
"CNZZDATA1254194234=2076602214-1553782570-%7C1553782570"
} d= {
"geetest_challenge":"af4536ec53cb7935a0326b1c4031f1a0jt",
"geetest_validate":"49aec4801b8b9a125fa1473876b2f036",
"geetest_seccode":"49aec4801b8b9a125fa1473876b2f036|jordan"
}
s = requests.session()
r = s.post(url, headers=h, data=d, verify=False)
result = r.json()
print(type(result))
print(result["company"])
data = result["data"]
print(data)
print(data[0])
get_result = data[0]['context']
print(get_result)
if u"签收" in get_result:
print("快递单已签收成功")
else:
print("未签收")

运行后 print(result["company"]) 返回的结果为 None ,后面的返回错误:

python接口自动化-传 json 参数

一 一排查没找到原因,直到在 fiddle 中用 Composer 发送请求后,查看返回后的结果才明白,具体如下:

1.用composer发送请求(将之前发送成功的请求拖至此处)

python接口自动化-传 json 参数

2.查看返回的结果

python接口自动化-传 json 参数

实际上这个接口在fiddler上都没有跑通,用如下代码验证:

#coding:utf-8
import requests
import json
# import urllib3
# urllib3.disable_warnings()
url = "http://www.kuaidi.com/index-ajaxselectcourierinfo-75135471609813-zhongtong.html"
h = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/70.0.3538.110 Safari/537.36",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Referer":"http://www.kuaidi.com/",
"Cookie":"lang=zh-cn; theme=default; sid=ngr6ji5prpj85cmj7lncnccap7; "
"UM_distinctid=169c4b016739f-069c297f9ff0e7-3a3a5d0c-100200-169c4b016752ec; "
"CNZZDATA1254194234=2076602214-1553782570-%7C1553782570"
} d= {
"geetest_challenge":"af4536ec53cb7935a0326b1c4031f1a0jt",
"geetest_validate":"49aec4801b8b9a125fa1473876b2f036",
"geetest_seccode":"49aec4801b8b9a125fa1473876b2f036|jordan"
}
s = requests.session()
r = s.post(url, headers=h, data=d, verify=False)
result = r.json()
print(type(result))
print(result["company"])
print(result["companytype"])
print(result["reason"])

 

返回的结果如下:

python接口自动化-传 json 参数

与 fiddler 返回的结果一致

所以是这个接口再次请求的时候就出现问题了,有兴趣可以再换一个近期的快递单号试试

本次实操收获:以后在做接口测试的时候尽量先在fiddler 的 Compser上请求一次,看下能不能先跑通,以免出现问题的时候花大量的时间来排查!!