Postman和Python Request测试多行Form-data

1、请求参数有多个,F12查看请求体如下:

查看源代码:

html 复制代码
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="custId"

IICON004
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="custName"

zljun8210@live.cn
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="workOrderId"

396215
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="comment"

Yes
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="file"

undefined
------WebKitFormBoundaryHknGXm9VkhRUXZYC--

原始请求上的Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryHOwJrytA0cCkCKXb

2、 在Postman上添加HTTP请求,配置如下:
3、 Python代码处理如下:
python 复制代码
import requests
import urllib3
from requests_toolbelt import MultipartEncoder


def customerReply(custid, woid, comments):
    url = 'https://xxx.xxx.xxx/xxxxxx/api/workorder/addcomment'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'}

    data = MultipartEncoder({
        'custId': '%s' % custid,
        'custName': 'AutoReplyer',
        'workOrderId': '%s' % woid,
        'comment': '%s' % comments
    })

    headers['Content-Type'] = data.content_type
    urllib3.disable_warnings()
    resut = requests.post(url=url, headers=headers, data=data, verify=False)

    if resut.status_code == 200:
        # sys.stderr.write('customer replied !')
        return True
    else:
        return False


if __name__ == '__main__':
    customerReply('IICON004', 396215, 'customer reply test.')

注意:与普通的接口测试有区别的是,headers里面的Content-Type要取消,另外定义 data.content_type,请求后会自动处理。

如果还是用以下这种header,Post请求后会报405错误。

python 复制代码
headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
            'Content-Type': 'text/plain;charset=UTF-8'}

以上。

相关推荐
s1hiyu1 天前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472461 天前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤1 天前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板
Duang007_1 天前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
浒畔居1 天前
机器学习模型部署:将模型转化为Web API
jvm·数据库·python
抠头专注python环境配置1 天前
基于Pytorch ResNet50 的珍稀野生动物识别系统(Python源码 + PyQt5 + 数据集)
pytorch·python
百***78751 天前
Kimi K2.5开源模型实战指南:核心能力拆解+一步API接入(Python版,避坑全覆盖)
python·microsoft·开源
喵手1 天前
Python爬虫实战:针对天文历法网站(以 TimeandDate 或类似的静态历法页为例),构建高精度二十四节气天文数据采集器(附xlsx导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集天文历法网站数据·构建二十四节气天文数据
zhaotiannuo_19981 天前
Python之2.7.9-3.9.1-3.14.2共存
开发语言·python
Keep_Trying_Go1 天前
基于GAN的文生图算法详解ControlGAN(Controllable Text-to-Image Generation)
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·文生图