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

以上。

相关推荐
小陈工1 小时前
Python安全编程实践:常见漏洞与防护措施
运维·开发语言·人工智能·python·安全·django·开源
2401_874732537 小时前
为你的Python脚本添加图形界面(GUI)
jvm·数据库·python
FreakStudio8 小时前
0 元学嵌入式 GUI!保姆级 LVGL+MicroPython 教程开更,从理论到实战全搞定
python·单片机·嵌入式·面向对象·电子diy
剑穗挂着新流苏31210 小时前
117_PyTorch 实战:利用训练好的模型进行单张图片验证
人工智能·python·深度学习
Lethehong10 小时前
Python Selenium全栈指南:从自动化入门到企业级实战
python·selenium·测试工具·自动化
山里幽默的程序员10 小时前
Postman如何导出全部接口集合?备份与共享详细步骤
测试工具·postman
智算菩萨11 小时前
MP3音频编码原理深度解析与Python全参数调优实战:从心理声学模型到LAME编码器精细控制
android·python·音视频
qq_4523962311 小时前
【模型手术室】第四篇:全流程实战 —— 使用 LLaMA-Factory 开启你的第一个微调任务
人工智能·python·ai·llama
无心水12 小时前
Java时间处理封神篇:java.time全解析
java·开发语言·python·架构·localdate·java.time·java时间处理
吴秋霖12 小时前
【某音电商】protobuf聊天协议逆向
python·算法·protobuf