Sending csv file using Requests.PUT in python [400 Client error: Bad Request]
Question: I am trying to send a csv file using Request module but I keep getting "400
Client Error: BAD REQUEST for url" error. According to the specification that
I have, here is an example that was given for curl; `curl -X PUT -H "Content-
Disposition: attachment;filename=ABC.csv" -H "Content-Type: application/csv"
-T ABC.csv http://.../api/dss/sites/1/vardefs`

Below is my python code;

    
    
    import requests
    filepath = 'C:\...\ABC.csv'
    with open(filepath) as WA:
        mydata = WA.read()
        response = requests.put('http://...../api/dss/sites/1/vardefs',
                               data=mydata,                         
                               headers = {'content-type':'application/csv', 'Content-Disposition': 'attachment;filename=Cu_temp.csv'},              
                               params={'file': filepath}
                )
        response.raise_for_status()
    

Any idea on what I am doing wrong?


Answer: From 'requests' docs:

> data -- (optional) Dictionary, bytes, or file-like object to send in the
> body of the Request.

Try sending `WA` directly (without reading first) for a streaming upload
instead. Also, it is always recommended to open files in 'rb' (read binary)
mode when uploading with requests.

(Edit in response to a comment)

Something like this:

    
    
    import requests
    
    filepath = 'C:\...\ABC.csv'
    with open(filepath, 'rb') as WA:
        response = requests.put('http://...../api/dss/sites/1/vardefs',
                                data=WA,
                                headers = {
                                               'content-type':'application/csv',
                                               'Content-Disposition': 'attachment;filename=Cu_temp.csv'
                                          })
    

Does it work this time?

