Python接口自动化测试的实现( 二 )

输出:
http://httpbin.org/get?show_env=1{'origin': '183.14.133.88','headers': {'X-Request-Id': 'ebe922b4-c463-4fe9-9faf-49748d682fd7','Accept-Encoding': 'gzip,deflate','X-Forwarded-Port': '80','Total-Route-Time': '0','Connection': 'close','Connect-Time': '0','Via': '1.1vegur','X-Forwarded-For': '183.14.133.88','Accept': '*/*','User-Agent': 'python-requests/2.18.1','X-Request-Start': '1504755961007','Host': 'httpbin.org','X-Forwarded-Proto': 'http'},'args': {'show_env': '1'},'url': 'http: //httpbin.org/get?show_env=1'}3、带header的get:
# -*- coding:utf-8 -*- import requestsimport json host = "http://httpbin.org/"endpoint = "get" url = ''.join([host,endpoint])headers = {"User-Agent":"test request headers"} r = requests.get(url)r = requests.get(url,headers=headers)#response = r.json()print (eval(r.text))['headers']['User-Agent']输出:
test request headers4、同时带参数和header:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "get" url = ''.join([host,endpoint])headers = {"User-Agent":"test request headers"}params = {"show_env":"1"} r = requests.get(url)r = requests.get(url,headers=headers,params=params) #response = r.json()print (eval(r.text))['headers']['User-Agent']print r.url输出:
test request headershttp://httpbin.org/get?show_env=1(3)requests.post()
一、方法定义
二、post方法简单使用
1、带数据的post
2、带header的post
3、带json的post
4、带参数的post
5、普通文件上传
6、定制化文件上传
7、多文件上传
一、方法定义:
1、到官方文档去了下requests.post()方法的定义,如下:

Python接口自动化测试的实现

文章插图
 
2、源码:
Python接口自动化测试的实现

文章插图
 
3、常用返回信息:
Python接口自动化测试的实现

文章插图
 
二、post方法简单使用:
1、带数据的post:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post"url = ''.join([host,endpoint])data = https://www.isolves.com/it/cxkf/yy/Python/2020-09-17/{'key1':'value1','key2':'value2'} r = requests.post(url,data=data)#response = r.json()print (r.text)输出:
{"args": {},"data": "","files": {},"form": {"key1": "value1","key2": "value2"},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Connection": "close","Content-Length": "23","Content-Type": "application/x-www-form-urlencoded","Host": "httpbin.org","User-Agent": "python-requests/2.18.1"},"json": null,"origin": "183.14.133.88","url": "http://httpbin.org/post"}2、带header的post:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint])headers = {"User-Agent":"test request headers"} # r = requests.post(url)r = requests.post(url,headers=headers)#response = r.json()输出:
{"args": {},"data": "","files": {},"form": {},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Connection": "close","Content-Length": "0","Host": "httpbin.org","User-Agent": "test request headers"},"json": null,"origin": "183.14.133.88","url": "http://httpbin.org/post"}3、带json的post:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint])data = https://www.isolves.com/it/cxkf/yy/Python/2020-09-17/{"sites": [{ "name":"test" , "url":"www.test.com" },{ "name":"google" , "url":"www.google.com" },{ "name":"weibo" , "url":"www.weibo.com" }]} r = requests.post(url,json=data)# r = requests.post(url,data=json.dumps(data))response = r.json()


推荐阅读