Postman的Binary方式传送数据,requests实现

问题来源及背景

最近在学习ES,在批量上传数据的时候卡住了,requests上传文件的时候一般用的是

url = 'http://127.0.0.1:9200/_bulk'
files = {
      "field" : open(filename, mode='rb')
    }
requests.post(url, data=files, headers={'Content-Type':'binary'})

但是上传的过程中ES一直报错,信息如下

{"error":{"root_cause":[{"type":"parse_exception","reason":"Failed to derive 
xcontent"}],"type":"parse_exception","reason":"Failed to derive xcontent"},"status":400}

但是利用postman的binary上传时却是正确的,如下图

解决

多方查找后并没有结果,后来去requests的官方文档上找到了一种解决方式:

with open(filename, 'rb') as f:
    r = requests.post(url, data=f)

原文:

Streaming Uploads

Requests supports streaming uploads, which allow you to send large streams or files without reading them into memory. To stream and upload, simply provide a file-like object for your body:

with open('massive-body', 'rb') as f:
    requests.post('http://some.url/streamed', data=f)
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值