How to post a file to server using Python that mimics the behavior of this R code?

Viewed 41

I'm able to succesfully POST some csv data to an external server using the httr package in R using a post request like so:

request <- POST(url, body = upload_file('my_table.csv'), verbose())

The detail provided by the verbose() option above tells me that the post request headers look like this:

-> User-Agent: libcurl/7.68.0 r-curl/4.3.1 httr/1.4.2
-> Accept-Encoding: deflate, gzip, br
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: text/csv
-> Content-Length: 77
-> 

What I'm trying to do is emulate this code with the Python requests module (because the rest of the package is in Python), and I'm using the following code:

response = requests.post(url, files = {'file': open('my_table.csv','rb')})

However, I'm getting a error,Error: File type not supported error from the server when I do so. When I look at the details of my POST request in Python, I see the following headers:

{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '226', 'Content-Type': 'multipart/form-data; boundary=b8f99a72145547743d035be5d9c1e983'}

What is the cleanest way for me to upload and post this CSV data such that the server might accept it?

0 Answers
Related