Postman和Python Request测试多行Form-data

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

查看源代码:

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

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

[email protected]
------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'}

以上。

相关推荐
满怀101535 分钟前
【Flask全栈开发指南】从零构建企业级Web应用
前端·python·flask·后端开发·全栈开发
PWRJOY36 分钟前
Flask-SQLAlchemy_数据库配置
数据库·python·flask
mahuifa1 小时前
Qt图表绘制(QtCharts)- 性能优化(13)
python·qt·pyside6·开发经验·qtchart
Bugabooo1 小时前
python打卡DAY22
开发语言·python
低维歌者2 小时前
python训练营day27
java·开发语言·python
微刻时光2 小时前
影刀处理 Excel:智能工具带来的高效变革
人工智能·python·低代码·自动化·excel·rpa·影刀rpa
大帅不是我2 小时前
Python多进程编程执行任务
java·前端·python
Fu_lucas2 小时前
Python Logging 模块完全指南
开发语言·python
Eiceblue2 小时前
Python 在Excel单元格中应用多种字体样式
开发语言·vscode·python·pycharm·excel
Superstarimage4 小时前
使用conda创建python虚拟环境,并自定义路径
windows·python·conda