Python接口测试-使用requests模块发送post请求

本篇主要记录下使用python的requests模块发送post请求的实现代码.

#coding=utf-8
import unittest
import requests

class PostTest(unittest.TestCase):

    def setUp(self):
        host = ‘https://httpbin.org/‘
        endpoint = ‘post‘
        self.url = ‘‘.join([host, endpoint])

    def testPost(self):
        params = {‘show_env‘:‘1‘}
        json = {
            ‘info‘: {‘show_env‘: ‘2‘, ‘sex‘: ‘nv‘},
            ‘code‘: 200,
            ‘a‘: ‘hello‘, ‘b‘: ‘nihao‘,
            ‘files‘ : {‘file‘: (‘test.txt‘, ‘hello‘)},
            ‘data‘: [{‘name‘: ‘zhangsan‘, ‘id‘: ‘123‘}, {‘name‘: ‘lisi‘, ‘id‘: ‘125‘}],
            ‘id‘: 1900
        }

        r1 = requests.post(self.url,params=params,json=json)
        resp1 = r1.json()
        print(resp1)
        connect = resp1[‘headers‘][‘Connection‘]
        self.assertEqual(connect, ‘close‘)

    def tearDown(self):
        pass

if __name__==‘__main__‘:
    unittest.main()
时间: 2024-07-31 03:26:53

Python接口测试-使用requests模块发送post请求的相关文章

python - 怎样使用 requests 模块发送http请求

最近在学python自动化,怎样用python发起一个http请求呢? 通过了解 request 模块可以帮助我们发起http请求 步骤: 1.首先import 下 request 模块 2.然后看请求的方式,选择对应的请求方法 3.接受返回的报文信息 例子:get 方法 import requests url ="https://www.baidu.com" res = requests.get(url) res.encoding = "utf-8" res.te

python接口测试中—Requests模块的使用

Requests模块的使用 中文文档API:http://2.python-requests.org/en/master/ 1.发送get.post请求 import requests reponse = requests.get("http://www.baidu.com") reponse = requests.post("http://www.baidu.com") 2.响应的属性 查看响应内容 response.text 属性 respone.content

python 爬虫 基于requests模块的get请求

需求:爬取搜狗首页的页面数据 import requests # 1.指定url url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返回请求成功的响应对象 response = requests.get(url=url) # 3.获取响应中的数据:text属性作用是可以获取响应对象中字符串形式的页面数据 page_data = response.text # 4.持久化数据 with open("sougou.html","w&

Python接口测试,requests库的post请求进行文件上传

前言 如果需要发送文件到服务器,比如上传图片.视频等,就需要发送二进制数据. 一般上传文件使用的都是 Content-Type: multipart/form-data; 数据类型,可以发送文件,也可以发送相关的消息体数据. POST一个多部分编码(Multipart-Encoded)的文件 使用 requests 上传文件的基本步骤 构造文件数据,通过 open 函数以二进制方式打开文件 构造相关数据 发送请求,将文件数据以  files  参数传入,其他消息体数据通过  data .json

Python接口测试,requests库的post请求进行文件下载

前言 之前讲了文件上传,当然就有文件下载啦 文件下载操作步骤 极其简单,将二进制格式的响应内容存进本地文件中,根据需要下载的文件的格式来写文件名即可 1 down_url = 'https://www.imooc.com/mobile/appdown' 2 res = requests.post(down_url).content 3 with open("F:/imooc.apk", "wb") as f: 4 f.write(res) 原文地址:https://

Python接口测试,Requests模块讲解:GET、POST、Cookies、Session等

文章最下方有对应课程的视频链接哦^_^ 一.安装.GET,公共方法 二.POST 三.Cookies 四.Session 五.认证 六.超时配置.代理.事件钩子 七.错误异常

python之使用request模块发送post和get请求

import requestsimport json #发送get请求并得到结果# url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马 '#请求接口# req = requests.get(url)#发送请求# print(req.text)#获取请求,得到的是json格式# print(req.json())#获取请求,得到的是字典格式# print(type(req.text))# print(type(req.json()))

python 爬虫 基于requests模块发起ajax的get请求

基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下下滚轮拖动页面,会加载更多的电影信息,这个局部刷新是当前页面发起的ajax请求, 用抓包工具捉取页面刷新的ajax的get请求,捉取滚轮在最底部时候发起的请求 这个get请求是本次发起的请求的url ajax的get请求携带参数 获取响应内容不再是页面数据,是json字符串,是通过异步请求获取的电影

基于python第三方requests 模块的HTTP请求类

使用requests模块构造的下载器,首先安装第三方库requests pip install requests 1 class StrongDownload(object): 2 3 def __init__(self): 4 #拿到代理iplist 5 self.iplist = ['自己想办法搞'] # 6 self.UserAgent = ['自己想办法搞'] 7 8 def get(self,url,timeout,proxy=False,num_retries=3): 9 '''ur