问题来源及背景
最近在学习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)
解决Elasticsearch批量上传数据时的解析错误

博主在学习Elasticsearch(ES)过程中遇到批量上传数据的问题,使用requests库时出现‘Failed to derive xcontent’的400错误。尝试了多种方法未果,最后从requests官方文档中找到解决方案,通过直接提供文件对象而非字典形式的数据解决了问题。此篇博客分享了解决这一问题的具体步骤和代码示例。
1万+





