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

以上。

相关推荐
Hgfdsaqwr23 分钟前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
开发者小天33 分钟前
python中For Loop的用法
java·服务器·python
老百姓懂点AI1 小时前
[RAG实战] 向量数据库选型与优化:智能体来了(西南总部)AI agent指挥官的长短期记忆架构设计
python
喵手3 小时前
Python爬虫零基础入门【第九章:实战项目教学·第15节】搜索页采集:关键词队列 + 结果去重 + 反爬友好策略!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·搜索页采集·关键词队列
Suchadar3 小时前
if判断语句——Python
开发语言·python
ʚB҉L҉A҉C҉K҉.҉基҉德҉^҉大3 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
喵手4 小时前
Python爬虫零基础入门【第九章:实战项目教学·第14节】表格型页面采集:多列、多行、跨页(通用表格解析)!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·表格型页面采集·通用表格解析
0思必得04 小时前
[Web自动化] 爬虫之API请求
前端·爬虫·python·selenium·自动化
莫问前路漫漫4 小时前
WinMerge v2.16.41 中文绿色版深度解析:文件对比与合并的全能工具
java·开发语言·python·jdk·ai编程
木头左4 小时前
Backtrader框架下的指数期权备兑策略资金管理实现与风险控制
python