python接口自动化-传json参数
一、post请求有两种方法传json参数:
- 1.传json参数(自动转 json )
- 2.传data参数(需 json 转换)
代码参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 单号,判断它的状态是不是已签收
实操中原本的理想代码,实际没不通:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #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 ,后面的返回错误:
一 一排查没找到原因,直到在 fiddle 中用 Composer 发送请求后,查看返回后的结果才明白,具体如下:
1.用composer发送请求(将之前发送成功的请求拖至此处)
2.查看返回的结果
实际上这个接口在fiddler上都没有跑通,用如下代码验证:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #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" ]) |
返回的结果如下:
与 fiddler 返回的结果一致
所以是这个接口再次请求的时候就出现问题了,有兴趣可以再换一个近期的快递单号试试
本次实操收获:以后在做接口测试的时候尽量先在fiddler 的 Compser上请求一次,看下能不能先跑通,以免出现问题的时候花大量的时间来排查!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下