Python requests multipart/form-data

multipart/form-data 一般用于文件流传输,请求数据比较大时使用

import requests
from collections import OrderedDict

url = "http://testgwpos.dmall.com/order/groupInfo"
# 构建有序字典参数按如下方式组织,也是模拟multipart/form-data的核心

params = OrderedDict([
    ("param", (None, '{"orderId":"53045085","nonceStr":"VSlwZKEJ157CqH24","posNo":"236","storeId":"112","channel":"cpos"}')),
    ("sign", (None, '55515FA6E5FB806E23A4BB55F291F5D4')),
])


res = requests.post(url, files=params)
print(res.request.body)
print(res.request.headers)
print(res.text)

Request Body:

--630fa44d7faead368db3a0d680b6d980
Content-Disposition: form-data; name="param"
{"orderId":"53045085","nonceStr":"VSlwZKEJ157CqH24","posNo":"236","storeId":"112","channel":"cpos"}

--630fa44d7faead368db3a0d680b6d980
Content-Disposition: form-data; name="sign"
55515FA6E5FB806E23A4BB55F291F5D4

--630fa44d7faead368db3a0d680b6d980--

Request Headers:

{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 
'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '340', 
'Content-Type': 'multipart/form-data; boundary=630fa44d7faead368db3a0d680b6d980'}

Response Body:

{"code":"GWPOS23103","msg":"非待付款订单,请前往服务台查询~(GWPOS23103)",
"sourceMsg":"非待付款订单,请前往服务台查询~","data":null,"success":false}
posted @ 2020-06-06 15:29  Onsunsl  阅读(525)  评论(0编辑  收藏  举报