python接口测试之路get、post、json请求处理
基于http协议,最常用的是GET和POST两种方法。
接口文档需要包含哪些信息:
接口名称
接口功能
接口地址
支持格式 json/xml
请求方式
请求示例
请求参数(是否必填、数据类型、传递参数格式)
返回参数说明
以典型的(一两个)参数做为判断是否请求通过(重点是看响应的信息判断)
一、GET
![](/assets/blank.gif)
1 import requests 2 import json 3 4 url = "http://v.juhe.cn/laohuangli/d" 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 6 header ={} 7 8 r = requests.get(url,params=para,headers= header) 9 10 print('get请求获取的响应结果json类型',r.text) 11 print("get请求获取响应状态码",r.status_code) 12 print("get请求获取响应头",r.headers['Content-Type']) 13 14 #响应的json数据转换为可被python识别的数据类型 15 json_r = r.json() 16 print(json_r)
![](/assets/blank.gif)
二、POST
post请求有两种请求格式:
1、key-value的格式'Content-Type':'application/x-www-form-urlencoded'
2、标准json的格式:'Content-Type':'application/json'
#key-value
![](/assets/blank.gif)
1 import requests 2 import json 3 4 url = "http://v.juhe.cn/laohuangli/d" 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 6 header ={} 7 8 r = requests.post(url,data=para,headers= header) 9 10 print('get请求获取的响应结果json类型',r.text) 11 print("get请求获取响应状态码",r.status_code) 12 print("get请求获取响应头",r.headers['Content-Type']) 13 14 #响应的json数据转换为可被python识别的数据类型 15 json_r = r.json() 16 print(json_r)
![](/assets/blank.gif)
#json
![](/assets/blank.gif)
1 import requests 2 import json 3 4 url = "http://v.juhe.cn/laohuangli/d" 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 6 header ={} 7 #python数据类型转换为json类型(json.dumps()) 8 para = json.dumps(para) 9 r = requests.post(url,data=para,headers= header) 10 11 print('get请求获取的响应结果json类型',r.text) 12 print("get请求获取响应状态码",r.status_code) 13 print("get请求获取响应头",r.headers['Content-Type']) 14 15 #响应的json数据转换为可被python识别的数据类型 16 json_r = r.json() 17 print(json_r)
![](/assets/blank.gif)
三、把所有的请求封装在函数中
![](/assets/blank.gif)
1 def get(url,para,headers): 2 try: 3 r = requests.get(url,params=para,headers=headers) 4 print("获取返回的状态码",r.status_code) 5 json_r = r.json() 6 print("json类型转化成python数据类型",json_r) 7 except BaseException as e: 8 print("请求失败!",str(e)) 9 def post(url,para,headers): 10 try: 11 r = requests.post(url,data=para,headers=headers) 12 print("获取返回的状态码",r.status_code) 13 json_r = r.json() 14 print("json类型转化成python数据类型",json_r) 15 except BaseException as e: 16 print("请求失败!",str(e)) 17 def post_json(url,para,headers): 18 try: 19 data = para 20 data = json.dumps(data) #python数据类型转化为json数据类型 21 r = requests.post(url,data=data,headers=headers) 22 print("获取返回的状态码",r.status_code) 23 json_r = r.json() 24 print("json转换为python数据类型:",json_r) 25 except BaseException as e: 26 print("请求失败!",str(e)) 27 28 url = "http://v.juhe.cn/laohuangli/d" 29 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 30 headers ={} 31 32 get(url,para,headers) 33 post(url,para,headers) 34 post_json(url,para,headers)
![](/assets/blank.gif)
四、把所有请求封装在一个对象里
![](/assets/blank.gif)
1 class Webrequests: 2 def get(self,url,para,headers): 3 try: 4 r = requests.get(url,params=para,headers=headers) 5 print("获取返回的状态码",r.status_code) 6 json_r = r.json() 7 print("json类型转化成python数据类型",json_r) 8 except BaseException as e: 9 print("请求失败!",str(e)) 10 def post(self,url,para,headers): 11 try: 12 r = requests.post(url,data=para,headers=headers) 13 print("获取返回的状态码",r.status_code) 14 json_r = r.json() 15 print("json类型转化成python数据类型",json_r) 16 except BaseException as e: 17 print("请求失败!",str(e)) 18 def post_json(self,url,para,headers): 19 try: 20 data = para 21 data = json.dumps(data) #python数据类型转化为json数据类型 22 r = requests.post(url,data=data,headers=headers) 23 print("获取返回的状态码",r.status_code) 24 json_r = r.json() 25 print("json类型转化成python数据类型",json_r) 26 except BaseException as e: 27 print("请求失败!",str(e)) 28 29 url = "http://v.juhe.cn/laohuangli/d" 30 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 31 headers ={} 32 33 q = Webrequests() 34 35 q.get(url,para,headers) 36 q.post(url,para,headers) 37 q.post_json(url,para,headers)
python接口测试之路get、post、json请求处理相关推荐
- python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
简介 有些 post 的请求参数是 json 格式的,这个前面发送post 请求里面提到过,需要导入 json模块处理.现在企业公司一般常见的接口因为json数据容易处理,所以绝大多数返回数据也是 j ...
- python接口测试之新手篇
嗨,大家好,我是小白,好久没写博客了,最近公司搞什么python的接口测试,心里一阵狂喜,在公司上百个接口里拿出一个主要接口一顿乱搞,好在搞通了 但是在这过程中也碰到了好多的问题,决定将问题分享出来能 ...
- Python接口测试之moco
在现在的软件开发过程中,特别是app的部分,需要的很多数据以及内容,都是来自server端的API,但是不能保证 在客户端开发的时候,api在server端已经开发完成,专门等着前端来调用,理想的情况 ...
- Python 接口测试之处理转义字符的参数和编码问题
目录 引言 处理入参有转义字符的接口 编码处理 接口请求方式 提取报文中参数 附录 引言 笔者,在非常繁忙的工作之余,决定抽时间记录一下在测试接口时所遇到的问题,以便日后参考,也可以提供给那些正在学习 ...
- Python接口测试之requests
1.简介 requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时 经常会用到,Requests是Python语言的第三方的库,专门用于发送HTTP请求 2.前提 ...
- python接口测试之requests详解_Python接口测试-requests库
一.requests库 Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量 ...
- python接口测试之request请求二(传递参数)
1.在请求中加参数时,一般是在url后面接?,然后传入key/value对,在requests中,一般使用params关键字参数的方式,以dict的形式传递url参数 #-*- coding:utf- ...
- python 接口传递参数params | data | json
params 使用params传递参数主要是用于get请求,传递的参数会自动拼接到链接中. data = {'btypeId': 15} res = requests.get(url=self.url ...
- 接口测试之主流协议测试合集(python版)
http://t.csdn.cn/U4WHz 接口测试之主流协议测试合集(python版) 01 TCP/IP四层模型&OSI七层模型 02 IP协议 03 TCP协议 04 UDP协议 05 ...
最新文章
- c++将小数化为二进制_C/C+学习笔记:C语言实现任意进制转换,代码全解析!...
- Oracle过滤与排序
- vue 集成html5 plus - 懒懒de尐彪 - 博客园
- 为何苦命干活的人成不了专家?
- poj-2528线段树练习
- Mybatis的入门到精通这一篇文章就够了
- cr3格式是什么意思_尼康DX镜头是什么意思
- java讲师助教简历模板,大学助教个人简历
- cf723d Lakes in Berland
- 南京大学计算机学院英才计划,2020年“英才计划”工作实施方案
- 用户需求 ≠ 产品需求,如何做需求分析?
- AMOLED 结构及显示原理
- 详解DAO类(数据库操作对象)
- JS:中文输入法,日文输入法做最大位数Check的坑maxlength
- 1024*1024像素的图片,文件大小多少?
- OSChina 周日乱弹 ——书中自有颜如玉
- java工程师待遇_Java程序员的薪资水平如何?
- 离散信源信息量、平均信息量的计算
- 微搭医美美容小程序官方模板解析
- 一千万条数据去重_DB2千万级数据去重