Content-Type: multipart/form-data; boundary=33dde4791cfce43a7aa122d93ab066dc requests.post()

使用requests.post方法请求Content-Type: multipart/form-data格式的接口,今天试了很多次都跟实际接口的请求不一致,传入的数据格式对不上,主要原因是在headers头中不用再传入Content-Type: multipart/form-data,具体见例子

1、传入的参数每个字段以元祖的格式保存

parm2 ={
    "occur_time": (None,'2019-02'),
    "logistics_provider_id":( None,'483'),
    "logistics_way_basic_info_id":(None, ''),
    "batch_name": (None,'20190301'),
    "number_type":(None,'5'),
    "currency":(None,'49'),
    "actual_receive_date": (None,'2019-03-01 00:00:44'),
    "carriage_bill_period": (None,'1'),
    "settlement_days":(None, '30'),
    "bill_month_start":(None,'2018-11'),
    "bill_month_end":(None,'2018-11'),
    "bill_date":(None,'2018-11-11 (星期日)'),
    "bill_date":(None,'2018-11-11 (星期日)'),
}

2、传入的files字段以字典的格式保存

file_obj = {'file':("download.csv", open("D:\\pyScripts\\onlineStudy\\test\\download.csv", 'rb'), 'application/vnd.ms-excel')}

3、headers中不能不需要content-type

headers = {
   'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'Cookie': 'laravel_session=PQgimLhu6w5vxBNb2gdjpX9v8QVkLEWJjal0KKiV',
}

4、请求

res = requests.post(url='http://dev2.lms.hqygou.com/api/finance_statement', data=parm3,files=file_obj, headers=headers)
print(res.request.body)
发布了41 篇原创文章 · 获赞 1 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览