博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python接口自动化测试(三)-requests.post()
阅读量:7081 次
发布时间:2019-06-28

本文共 5367 字,大约阅读时间需要 17 分钟。

上一节介绍了  requests.get()  方法的基本使用,本节介绍  requests.post()  方法的使用:

 

本文目录:

一、方法定义

二、post方法简单使用

  1、带数据的post

  2、带header的post

  3、带json的post

  4、带参数的post

  5、普通文件上传

  6、定制化文件上传

  7、多文件上传

 

一、方法定义:

1、到去了下requests.post()方法的定义,如下:

 

2、源码:

 

3、常用返回信息:

 

二、post方法简单使用:

 1、带数据的post:

# -*- coding:utf-8 -*-import requestsimport jsonhost = "http://httpbin.org/"endpoint = "post"url = ''.join([host,endpoint])data = {
'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 jsonhost = "http://httpbin.org/"endpoint = "post" url = ''.join([host,endpoint])data = {    "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()

输出:

{  "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 jsonhost = "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 jsonhost = "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 jsonhost = "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)heman793

输出比较在,就不帖了。

 

7、多文件上传:

# -*- coding:utf-8 -*-import requestsimport jsonhost = "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 jsonhost = "http://httpbin.org/"endpoint = "post"url = ''.join([host,endpoint])#流式上传with open( 'test.txt' ) as f:    r = requests.post(url,data = 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"}

 

 

转载地址:http://eslml.baihongyu.com/

你可能感兴趣的文章
VMware虚拟机linux系统时间同步的解决办法
查看>>
fsck修复受损的文件系统
查看>>
迅雷Bolt的ClipSubBindBitmap函数特别说明
查看>>
『Island 基环树直径』
查看>>
『点分治及其简单运用』
查看>>
ios app分享的url在微信中打开app分析
查看>>
HPUX cstm无法使用
查看>>
我的友情链接
查看>>
Linux网络管理工具
查看>>
Linux磁盘管理7
查看>>
【学神】 1-3xmanager远程工具的使用和vim编辑器的使用
查看>>
IBM 把 Informix 数据库给“卖了”
查看>>
maven在windows和Linux下的安装
查看>>
如何解锁Oracle数据库中账号
查看>>
C# Socket编程笔记(自己看,转载)
查看>>
UML建模工具Visual Paradigm(VP-UML)使用教程:安装详解
查看>>
zabbix -- 自定义key
查看>>
ubuntu下 wine如何解决 rtx乱码问题
查看>>
第5章分布式系统模式 Singleton
查看>>
清明节回安徽扫墓的行程和时间
查看>>