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

输出:
{"args": {},"data": "{"sites": [{"url": "www.test.com", "name": "test"}, {"url": "www.google.com", "name": "google"}, {"url": "www.weibo.com", "name": "weibo"}]}","files": {},"form": {},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Connection": "close","Content-Length": "140","Content-Type": "application/json","Host": "httpbin.org","User-Agent": "python-requests/2.18.1"},"json": {"sites": [{"name": "test","url": "www.test.com"},{"name": "google","url": "www.google.com"},{"name": "weibo","url": "www.weibo.com"}]},"origin": "183.14.133.88","url": "http://httpbin.org/post"}4、带参数的post:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint])params = {'key1':'params1','key2':'params2'} # r = requests.post(url)r = requests.post(url,params=params)#response = r.json()print (r.text)输出:
{"args": {"key1": "params1","key2": "params2"},"data": "","files": {},"form": {},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Connection": "close","Content-Length": "0","Host": "httpbin.org","User-Agent": "python-requests/2.18.1"},"json": null,"origin": "183.14.133.88","url": "http://httpbin.org/post?key2=params2&key1=params1"}5.普通文件上传:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint])#普通上传files = {'file':open('test.txt','rb')} r = requests.post(url,files=files)print (r.text)输出:
{"args": {},"data": "","files": {"file": "hello world!n"},"form": {},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Connection": "close","Content-Length": "157","Content-Type": "multipart/form-data; boundary=392865f79bf6431f8a53c9d56c62571e","Host": "httpbin.org","User-Agent": "python-requests/2.18.1"},"json": null,"origin": "183.14.133.88","url": "http://httpbin.org/post"}6.定制化文件上传:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint])#自定义文件名,文件类型、请求头files = {'file':('test.png',open('test.png','rb'),'image/png')} r = requests.post(url,files=files)print (r.text)heman7937.多文件上传:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint])#多文件上传files = [('file1',('test.txt',open('test.txt', 'rb'))),('file2', ('test.png', open('test.png', 'rb')))] r = requests.post(url,files=files)print (r.text)8.流式上传:
# -*- coding:utf-8 -*-import requestsimport json host = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint]) #流式上传with open( 'test.txt' ) as f:r = requests.post(url,data = https://www.isolves.com/it/cxkf/yy/Python/2020-09-17/f) print (r.text)输出:
{"args": {},"data": "hello world!n","files": {},"form": {},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Connection": "close","Content-Length": "13","Host": "httpbin.org","User-Agent": "python-requests/2.18.1"},"json": null,"origin": "183.14.133.88","url": "http://httpbin.org/post"}(4)Cookie&Session
掌握了前面几节的的内容,就可以做一些简单的http协议接口的请求发送了,但是这些还不够 。HTTP协议是一个无状态的应用层协议,也就是说前后两次请求是没有任何关系的,那如果我们测试的接口之前有相互依赖关系怎么办呢(比如我要在博客园发文章,是需要先登录的),这时我们就要用到cookie和session技术来保持客户端与服务器端连接的状态,这也就是本节要介绍的内容:


推荐阅读