kwn kwn - 1 year ago 90
Python Question

Issue while sending a file to an API

I'm trying to send a file to an API and then get the response - a CSV file
(I've seen different posts about it but I couldn't make it work)

The examples in the documentation use httpie

http --timeout 600 -f POST data@path/to/file.csv

but when I'm using requests, I get an
400 Bad Request

path = '/myfile.csv'
url = ''
files = {'file': open(path, 'rb')}
res =, data=files)

Answer Source

You need to specify files keyword argument, not data to post multipart/form-data request.

And the key should match: file -> data

path = 'path/to/file.csv'
url = ''
files = {'data': open(path, 'rb')}
#        ^^^^^^
res =, files=files)
#                        ^^^^^
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download