Uploading from requests library in python

Suggestions for WiGLE/JiGLE/DiGLE

4 posts • Page 1 of 1

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.
Image
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'})}
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
Image
I use `requests` and `dotenv`.

https://github.com/takano32/wigle-tools/

Code: Select all

#!/usr/bin/env python3 # import os from sys import argv import requests from dotenv import load_dotenv from requests.auth import HTTPBasicAuth dotenv_path = os.path.join(os.path.dirname(__file__), ".env") load_dotenv(dotenv_path) WIGLE_API_NAME = os.environ.get("WIGLE_API_NAME") or "" WIGLE_API_TOKEN = os.environ.get("WIGLE_API_TOKEN") or "" def upload(file_path) -> requests.Response: url = "https://api.wigle.net/api/v2/file/upload" headers = {"Accept": "application/json"} auth = HTTPBasicAuth(WIGLE_API_NAME, WIGLE_API_TOKEN) file_name = os.path.basename(file_path) file = open(file_path, "rb") file_data = file.read() file.close() files = {"file": (file_name, file_data)} data = {"donate": False} response = requests.post(url, headers=headers, auth=auth, files=files, data=data) return response file_paths = argv[1:] for file_path in file_paths: response = upload(file_path) print("--") print("status code:" + str(response.status_code)) print(response.content)

4 posts • Page 1 of 1

Return to “WiGLE Project Suggestions”

Who is online

Users browsing this forum: Google [Bot] and 2 guests