Page 1 of 1

Uploading from requests library in python

Posted: Thu Apr 23, 2020 2:57 pm
by Babyeggz

Code: Select all

url = 'https://api.wigle.net/api/v2/file/upload'

Code: Select all

data = {'file': (outfile, 'multipart/form-data', {'Expires': '0'})}

Code: Select all

response = requests.post(url, files=data, headers={'Authorization': 'Basic blahblahblah'})

Code: Select all

print(" ")

Code: Select all

response = response.text

Code: Select all

print(response)
(sry for bad formatting not sure how to post multi-line code snippets nicely)

I'm trying to upload my .csv from python via the requests library rather than using curl however with requests I keep getting {"code":400,"message":"HTTP 400 Bad Request"} so I must be doing something wrong. I can get it to upload via curl using subprocess but catching the output to see if it succeeded in the upload or failed is giving me troubles so I opted for using requests (plus its a little bit nicer)

Anything blatant that I'm doing wrong? I'm not a huge programmer i just make little projects here and there so i'm still learning.

Re: Uploading from requests library in python

Posted: Thu Apr 23, 2020 6:17 pm
by arkasha
You're close! Multipart wants a name.
Try something like:

Code: Select all

data = {'file': ('outfile_name.csv', open('path/to/file/outfile_name.csv', 'rb'), 'multipart/form-data', {'Expires': '0'})}

Re: Uploading from requests library in python

Posted: Thu Apr 23, 2020 8:28 pm
by Babyeggz
Oh my goodness! I was close! I got it all working now. I'll have to make a post or something to share the project i'm working on, I think its kinda neat.
Thanks so much Arkasha