I am attempting to upload a CSV file to an API (which gives me very little error info) using python requests library.
(I'm running Python 3.5 and using requests version 2.18.4 on OS X 10.11.6)
This curl command in terminal works fine: curl -F 'file=@/path/to/file.csv' myurl.com/upload -H "Authorization: TOKEN sometoken"
A multipart/form-data POST request from Postman also works, but I can't seem to make it work with the python requests library.
I've tried many variations of this request:
import requests
headers = {'Authorization': 'TOKEN sometoken', 'Content-Type': 'multipart/form-data'}
with open(file_path, 'rb') as f:
r = requests.post(myurl, headers=headers, data=f)
## I've also tried data={"file": f}
I get a status code of 200, but the response is {"success": "false"}
(frustratingly unhelpful).
What am I missing in the python request compared to the curl request?
EDIT: it seems that the -F
flag for the curl
command emulates an HTML form that has been submitted...is there a way to do this with requests?