python requests upload large file with additional data

后端 未结 1 1479
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 17:00

I\'ve been looking around for ways to upload large file with additional data, but there doesn\'t seem to be any solution. To upload file, I\'ve been using this code and it\'s be

1条回答
  •  抹茶落季
    2021-02-07 17:19

    You can use the requests-toolbelt to do this:

    import requests
    from requests_toolbelt.multipart import encoder
    
    session = requests.Session()
    with open('my_file.csv', 'rb') as f:
        form = encoder.MultipartEncoder({
            "documents": ("my_file.csv", f, "application/octet-stream"),
            "composite": "NONE",
        })
        headers = {"Prefer": "respond-async", "Content-Type": form.content_type}
        resp = session.post(url, headers=headers, data=form)
    session.close()
    

    This will cause requests to stream the multipart/form-data upload for you.

    0 讨论(0)
提交回复
热议问题