python requests upload large file with additional data

后端 未结 1 806
名媛妹妹
名媛妹妹 2021-02-07 16:43

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:10

    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)
提交回复
热议问题