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

上一节介绍了  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"
}
时间: 2024-12-24 21:56:57

python接口自动化测试(二)-requests.post()的相关文章

python接口自动化测试(二)-requests.get()

环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括: requests常用请求方法使用,包括:get,post requests库中的Session.Cookie的使用 其它高级部分:认证.代理.证书验证.超时配置.错误异常处理等. 本节首先来了解一下requests库中如何发送get请求: 一.看下方法定义: 1.到官方文档去了下requests.get()方法的定义,如下: 2.点击右上角的[source],看一下它的源码如下: 看到最后一行return,get方法

python接口自动化测试二:python代码实现接口测试

url = '接口地址' r = requests.get(url)           #发送get请求 print(r.status_code)            #打印状态码,若有重定向,返回的是重定向之后的代码 print(r.headers)                #打印返回的报头(头部) print(r.text)                   #查看返回结果的文本形式 r.status_code                #响应状态码 r.content   

python接口自动化测试二:常用操作

url = '接口地址' r = requests.get(url)                      # 发送get请求 print(r.status_code)              # 打印状态码,若有重定向,返回的是重定向之后的代码 print(r.headers)                 # 打印返回的报头(头部) print(r.text)                    # 查看返回结果的文本形式 r.status_code               

python接口自动化测试二十三:文件上传

# 以禅道为例: 一.创建一个类,类里面写一个登录方法: import requestsclass LoginZentao(): def __init__(self, s): # 初始化 self.s = s # 定义一个全局的s def login(self): r = self.s.post() # self.s调用全局的s pass if __name__=='__main__': s = requests.session() zentao = LoginZentao(s) # 类实例化为

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

上一节介绍了  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:

python接口自动化测试二十五:执行所有用例,并生成HTML测试报告

    import requestsimport unittest class TestQQ(unittest.TestCase):    '''测试QQ号接口'''      # 此注释将展示到测试报告的测试组类 def test_qq(self):        '''测试QQ号码,正确的appkey'''      # 此注释将展示到测试报告的用例标题        url = 'http://japi.juhe.cn/qqevaluate/qq'        par = {     

python 接口自动化测试二(request.get)

环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括: requests常用请求方法使用,包括:get,post requests库中的Session.Cookie的使用 其它高级部分:认证.代理.证书验证.超时配置.错误异常处理等. 本节首先来了解一下requests库中如何发送get请求: 一.看下方法定义: 1.到官方文档去了下requests.get()方法的定义,如下: 2.点击右上角的[source],看一下它的源码如下: 看到最后一行return,get方法

python接口自动化测试二十六:使用pymysql模块链接数据库

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time    : 2018/5/28 18:51# @Author  : StalloneYang# @File    : mysql_test.py# @desc: # 连接数据库 import pymysql.cursors # 连接MySQL数据库connection = pymysql.connect(host='localhost', port=3306, user='yang', pass

python接口自动化测试二十:函数写接口测试

# coding:utf-8import requestsimport refrom bs4 import BeautifulSoup # s = requests.session() # 全局的s def get_token(s): ''' fuction: 获取token args: s 参数 ->s = requests.session() :return anti_token ->{'X-Anit-Forge-Token': 'xx', 'X-Anit-Forge-Code': '38