上一节介绍了  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 requests
import json host = "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 requests
import 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 requests
import json host = "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 requests
import 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 requests
import 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 requests
import 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)heman793

输出比较在,就不帖了。

7、多文件上传:

# -*- coding:utf-8 -*-
import requests
import 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 requests
import json host = "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"
}

接口自动化测试 (三)request.post的更多相关文章

  1. 接口自动化测试unittest+request+excel(一)

    注: 学习python自动化测试,需要先学习python基础,主要还是多敲代码,多联系,孰能生巧,你也会是一名合格的程序员 python基础学习: http://c.biancheng.net/pyt ...

  2. python接口自动化测试三十三:获取时间戳(10位和13位)

    很多时候,在调用接口时,需要对请求进行签名.需要用到unix时间戳. 在python里,在网上介绍的很多方法,得到的时间戳是10位.而java里默认是13位(milliseconds,毫秒级的). 下 ...

  3. python接口自动化测试(三)-requests.post()

    上一节介绍了  requests.get()  方法的基本使用,本节介绍  requests.post()  方法的使用: 本文目录: 一.方法定义 二.post方法简单使用 1.带数据的post 2 ...

  4. python接口自动化测试(一)-request模块

    urllib.request模块是python3针对处理url的. 1. 首先导入: from urllib import request 2. 构造url,构造url的headers信息和传参[re ...

  5. python接口自动化测试三:代码发送HTTP请求

    get请求: 1.get请求(无参数): 2.get请求(带参数): 接口地址:http://japi.juhe.cn/qqevaluate/qq 返回格式:json 请求方式:get post 请求 ...

  6. python接口自动化测试三十六:数据驱动参数化之paramunittest

    官方文档1.官方文档地址:https://pypi.python.org/pypi/ParamUnittest/2.github源码下载地址:https://github.com/rik0/Param ...

  7. python接口自动化测试三十五:用BeautifulReport生成报告

    GitHub传送门:https://github.com/TesterlifeRaymond/BeautifulReport 配置BeautifulReport 下载.解压并修改名字为Beautifu ...

  8. python接口自动化测试三十四:github上某接口测试平台及配置

    TeserHome地址:https://testerhome.com/opensource_projects/60前端:https://github.com/pencil1/ApiTestWeb 实现 ...

  9. 【python3+request】python3+requests接口自动化测试框架实例详解教程

    转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...

  10. python+request+HTMLTestRunner+unittest接口自动化测试框架

    转自https://my.oschina.net/u/3041656/blog/820023 正在调研使用python进行自动化测试,在网上发现一篇比较好的博文,作者使用的是python3,但目前自己 ...

随机推荐

  1. Go语言排序算法实现

    // Algorithm project Algorithm.go package Algorithm // 冒泡排序 func BubbleSort(a []int) { n := len(a) ; ...

  2. curl命令转换成php源码

    curl命令转换成php源码 获取状态: curl -X GET -H "Content-Type:application/json" -H "Authorization ...

  3. Pilosa文档翻译(三)示例

    目录 简单说明 Introduction 数据模型 Data Model 映射Mapping 0列(colums) --> 1字段(field) 1列(colums) --> 1字段(fi ...

  4. 记一次免费让网站启用HTTPS的过程

    写在前面 个人网站运行将近2个月了,期间根据酷壳的一篇教程如何免费的让网站启用HTTPS做了一次,中间遇到问题就放下了.昨天孙三苗问我网站地址说要添加友链,出于好奇想看他网站长什么样,顺道也加一下友链 ...

  5. markdown列表中的缩进

    使用markdown时,一向不敢使用多级列表,因为多级列表容易出错.而实际上,只需要缩进两个空格就可以实现嵌套了. * one 这个是one的内容 * two 这个是two的内容 * three * ...

  6. 简单理解 RPC(转载)

    原文地址:http://www.syyong.com/architecture/Simple-understanding-of-RPC.html RPC(Remote Procedure Call P ...

  7. STM32F105 PA9/OTG_FS_VBUS Issues

    https://www.cnblogs.com/shangdawei/p/3264724.html F105 DFU模式下PA9引脚用来检测USB线缆,若电平在2.7~5v则认为插入usb设备(检测到 ...

  8. Python3 字符串前面加u,r,b的含义

    u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. 一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u:但是中文, ...

  9. Cobalt Strike DNS通讯实例

    一.域名设置 如果没有域名,可以参考另一篇博客,申请Freenom免费域名,并使用DNSPod解析 链接:https://www.cnblogs.com/ssooking/p/6364639.html ...

  10. 解决ubuntu系统中firefox无法播放网页版音乐播放器音乐

    Reference: https://blog.csdn.net/h736131708/article/details/80775382 因为网页版的qq音乐或者网易云音乐都把音频换成了AAC格式,这 ...