一、方法定义

二、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": "",
"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": "",
"Host": "httpbin.org",
"User-Agent": "test request headers"
},
"json": null,
"origin": "183.14.133.88",
"url": "http://httpbin.org/post"

3、带json的post:

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\": [{\"name\": \"test\", \"url\": \"www.test.com\"}, {\"name\": \"google\", \"url\": \"www.google.com\"}, {\"name\": \"weibo\", \"url\": \"www.weibo.com\"}]}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": {
"sites": [
{
"name": "test",
"url": "www.test.com"
},
{
"name": "google",
"url": "www.google.com"
},
{
"name": "weibo",
"url": "www.weibo.com"
}
]
},
"origin": "113.65.130.163, 113.65.130.163",
"url": "https://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": "",
"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": "",
"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": "",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "183.14.133.88",
"url": "http://httpbin.org/post"
}

pyhton接口自动化测试-requests.post()的更多相关文章

  1. 记录python接口自动化测试--requests使用和基本方法封装(第一目)

    之前学习了使用jmeter+ant做接口测试,并实现了接口的批量维护管理(大概500多条用例),对"接口"以及"接口测试"有了一个基础了解,最近找了一些用pyt ...

  2. python接口自动化测试 - requests库的post请求进行文件上传

    前言 如果需要发送文件到服务器,比如上传图片.视频等,就需要发送二进制数据. 一般上传文件使用的都是 Content-Type: multipart/form-data; 数据类型,可以发送文件,也可 ...

  3. python接口自动化测试 - requests库的post请求进行文件下载

    前言 之前讲了文件上传,当然就有文件下载啦 文件下载操作步骤 极其简单,将二进制格式的响应内容存进本地文件中,根据需要下载的文件的格式来写文件名即可 down_url = 'https://www.i ...

  4. python接口自动化测试 - requests库的基础使用

    简单介绍 requests库简单易用的HTTP库 Get请求 格式: requests.get(url) 注意:若需要传请求参数,可直接在 url 最后的 ? 后面,也可以调用 get() 时多加一个 ...

  5. 使用python+requests+unittest实现接口自动化测试

    这两天一直在找直接用python做接口自动化的方法,在网上也搜了一些博客参考,今天自己动手试了一下. 一.整体结构 上图是项目的目录结构,下面主要介绍下每个目录的作用. Common:公共方法:主要放 ...

  6. python3+requests+unittest:接口自动化测试(一)

    转载请表明出处:https://www.cnblogs.com/shapeL/p/9179484.html 简单介绍框架的实现逻辑,参考代码的git地址: https://github.com/zha ...

  7. python3+requests:接口自动化测试(二)

    转载请注明出处:https://www.cnblogs.com/shapeL/p/9188495.html 前言:上篇文章python3+requests+unittest:接口自动化测试(一):ht ...

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

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

  9. python+requests接口自动化测试

    转自https://my.oschina.net/u/3041656/blog/820023 原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测 ...

随机推荐

  1. LeetCode 19. 删除链表的倒数第N个节点(Remove Nth Node From End Of List)

    题目描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后, ...

  2. python源码

    初学者 GitHub - kennethreitz/pip-pop: Tools for managing requirements files. GitHub - kennethreitz/envo ...

  3. TNS:could not resolve the connect identifier specified解决办法

    添加环境变量解决:TNS_ADMIN ->> D:\OracleDB\product\11.2.0\dbhome_1\NETWORK\ADMIN

  4. java之中PriorityQueue实现原理(具有优先级的队列)

    使用大顶堆无限制大小.如果用顺序表实现,插入的时候麻烦,如果用链表(无序)实现得到最大优先级数据的时候麻烦.使用堆可以使两者得到中和.Lucene使用小顶堆定长实现,对于大量数据处理有利.

  5. Java中this和super的用法总结(转)

    原文地址:https://www.cnblogs.com/hasse/p/5023392.html 这几天看到类在继承时会用到this和super,这里就做了一点总结,与各位共同交流,有错误请各位指正 ...

  6. idea -- spring datasource配置文件不显示datasource.properties文件对应属性的值,错误提示cannot resolve property key

    原文:https://yq.aliyun.com/articles/657711 点击 文件 顶部的 蓝色 MVC application context,修改为Local File

  7. nodejs 之简单web服务器

    1.service.js var http=require('http');//引入http模块 var fs=require('fs');//fs模块 var path=require('path' ...

  8. 无法在WEB服务器上启动调试,Web 服务器配置不正确

    访问IIS元数据库失败 思考可能是次序出了问题,解决 1.打开CMD,进入 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 2.输入 aspnet_regi ...

  9. 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_08.RequestMapping注解的作用

    用于建立请求URL和处理请求方法之间的对应关系. 增加一个testResuqestMapping方法来测试 把注解放在类上 服务器重新部署 再次重新部署 这次就可以请求到数据 了 注解放在类上:用来表 ...

  10. Python学习之==>循环

    1.While循环 # 循环的时候是在重复执行循环体里面的东西 # 在循环体里面遇到break,立即结束循环,不管循环有没有完 # 在循环体里面遇到continue,那么就结束本次循环,继续进行下一次 ...