Uploading from requests library in python
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)
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.
You're close! Multipart wants a name.
Try something like:
Try something like:
Code: Select all
data = {'file': ('outfile_name.csv', open('path/to/file/outfile_name.csv', 'rb'), 'multipart/form-data', {'Expires': '0'})}
I use `requests` and `dotenv`.
https://github.com/takano32/wigle-tools/
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)
Return to “WiGLE Project Suggestions”
Who is online
Users browsing this forum: No registered users and 0 guests