在做接口测试的时候我们会遇到过需要填写表单的形式,那么如何通过requests进行请求呢? 这里需要引入新的python的第3方库requests-toolbelt

requests-toolbelt

1、安装

pip install requests-toolbelt

requests---requests请求表单-LMLPHP

官方文档:https://pypi.org/project/requests-toolbelt/

源码:

这里包含了许多方法,我们慢慢的进行了解

# -*- coding: utf-8 -*-
"""
requests-toolbelt
================= See http://toolbelt.rtfd.org/ for documentation :copyright: (c) 2014 by Ian Cordasco and Cory Benfield
:license: Apache v2.0, see LICENSE for more details
""" from .adapters import SSLAdapter, SourceAddressAdapter
from .auth.guess import GuessAuth
from .multipart import (
MultipartEncoder, MultipartEncoderMonitor, MultipartDecoder,
ImproperBodyPartContentException, NonMultipartContentTypeException
)
from .streaming_iterator import StreamingIterator
from .utils.user_agent import user_agent __title__ = 'requests-toolbelt'
__authors__ = 'Ian Cordasco, Cory Benfield'
__license__ = 'Apache v2.0'
__copyright__ = 'Copyright 2014 Ian Cordasco, Cory Benfield'
__version__ = '0.9.1'
__version_info__ = tuple(int(i) for i in __version__.split('.')) __all__ = [
'GuessAuth', 'MultipartEncoder', 'MultipartEncoderMonitor',
'MultipartDecoder', 'SSLAdapter', 'SourceAddressAdapter',
'StreamingIterator', 'user_agent', 'ImproperBodyPartContentException',
'NonMultipartContentTypeException', '__title__', '__authors__',
'__license__', '__copyright__', '__version__', '__version_info__',
]

这里我们会用到MultipartEncoder方法

MultipartEncoder上传文件用法

  import requests
from requests_toolbelt import MultipartEncoder encoder = MultipartEncoder({'field': 'value',
'other_field', 'other_value'})
r = requests.post('https://httpbin.org/post', data=encoder,
headers={'Content-Type': encoder.content_type})

MultipartEncoder填写表格用法

from requests_toolbelt import MultipartEncoder
import requests m = MultipartEncoder(fields={'field0': 'value', 'field1': 'value'}) r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type': m.content_type})

小试牛刀

这里安静通过举个小栗子来更加理解requests-toolbelt的用法,这里通过禅道提bug的接口

1、抓取禅道提bug的接口,分析参数数据

填写bug列表,打开Fiddler进行抓包分析数据

requests---requests请求表单-LMLPHP

通过抓包数据分析,可以看出来这是个multipart/form-data的数据类型,下面是填写的参数信息,老规矩,一个个写下来(这里不清楚那个是必填项或者是需要的内容)。

requests---requests请求表单-LMLPHP

2、导入requests-toobelt使用MultipartEncoder方法

参照源码方式,一个个参数进行添加,然后修改标题和修改bug内容

# coding:utf-8
import requests
from requests_toolbelt import MultipartEncoder
# 接口地址
url = 'http://demo.zentao.net/bug-create-39-0-moduleID=0.html'
# 请求头
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
"Cookie":"__cfduid=dcc14bd65a47b718842d4b412e2d517721545125741; Hm_lvt_731bb611021e8720f1fc3035dfd2a44f=1572415581; Hm_lpvt_731bb611021e8720f1fc3035dfd2a44f=1572415585; zentaosid=n6m4ua9k7b2c0hte58mqfi2lc0; lang=zh-cn; device=desktop; theme=default; UM_distinctid=16e1b4fca6c56a-0522f3f1a973da-5f123917-100200-16e1b4fca6d67f; CNZZDATA4553360=cnzz_eid%3D557018590-1572412770-null%26ntime%3D1572412770; lastProduct=39; windowWidth=1366; windowHeight=625; preBranch=0; preProductID=39; bugModule=0; qaBugOrder=id_desc"
}
# 请求参数信息
data = MultipartEncoder(
fields=[
('product',""),
('branch',''),
('module',''),
('project',''),
('openedBuild[]','trunk'),
('assignedTo',''),
('deadline',''),
('type','codeerror'),
('os','windows'), # 操作系统
('browser','all'), # 浏览器
('title','这是bug的名称'),
('color',''),
('severity',''), # 严重级
('pri',''), # 优先级
('steps','<p>[步骤]</p>\n<p>测试步骤第一步</p>\n<br />\n<p>[结果]</p>这里测试步骤第2步\n<br />\n<p>[期望]</p>\n<br />'),
('story',''),
('task',''),
('oldTaskID',''),
('mailto[]','dev1'),
('contactListMenu',''),
('keywords',''),
('status','active'),
('labels[]',''),
('files[]',''),
('uid','5db92b80efe7w'),
('case',''),
('caseVersion',''),
('caseVersion',''),
('result',''),
('testtask',''),
],
)
r = requests.post(url,headers={'Content-Type': data.content_type},data=data)

3、查看是否请求成功

具体的应该去数据库里看看数据有没有添加成功,这里安静模拟的接口,直接查看页面发现已经添加成功了

requests---requests请求表单-LMLPHP

通过一个小栗子让我们熟悉了requests-toolbelt的用法,当然不仅仅只有这一种方法,其他的方法小伙伴们可以自己摸索下,自己尝试写下

如果安静写的内容对您有帮助,可以右下角点个关注,持续更新;如果哪里写的有不对的地方,可以下方留言告诉安静,看到后第一时间回复哦~~

05-27 10:47