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

    科技2025-10-15  14

    问题来源及背景

    最近在学习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)
    Processed: 0.018, SQL: 8