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'}

以上。

相关推荐
wj3055853784 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李5 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154155 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君8 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
德思特9 小时前
通过 Wireshark 抓取串口命令
网络协议·测试工具·wireshark
PILIPALAPENG9 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户83562907805110 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL10 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
猫猫的小茶馆11 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32