48 lines
1.6 KiB
Python
48 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import sys
|
|
import json
|
|
import requests
|
|
|
|
def send_raw_csv_as_json(filename, api_url, bearer_token):
|
|
# Function to send raw CSV data as a JSON string
|
|
try:
|
|
# Read the CSV file and prepare it as a JSON string
|
|
with open(filename, 'r') as file:
|
|
csv_data = file.read() # Reading the CSV content as a string
|
|
|
|
payload = {
|
|
'json': csv_data # The key `json` with the CSV content as a string
|
|
}
|
|
print(payload)
|
|
|
|
headers = {
|
|
'Content-Type': 'application/json' # The API expects JSON payload
|
|
}
|
|
|
|
# Add the Authorization header if the bearer token is not empty
|
|
if bearer_token:
|
|
headers['Authorization'] = f'Bearer {bearer_token}'
|
|
else:
|
|
print("No Bearer token provided. Authorization header will be omitted.")
|
|
|
|
# Send the JSON data via POST request
|
|
response = requests.post(api_url, headers=headers, json=payload)
|
|
response.raise_for_status() # Raise an error for bad responses (4xx or 5xx)
|
|
|
|
# Log the response
|
|
print(f"Raw Data Response: {response.status_code} - {response.text}")
|
|
except Exception as e:
|
|
print(f"Error occurred while sending raw file as JSON: {e}")
|
|
|
|
def main():
|
|
if len(sys.argv) != 4:
|
|
print("Usage: python3 inoltroViaApiRaw.py <csv_filename> <api_url> <bearer_token>")
|
|
sys.exit(1)
|
|
filename = sys.argv[1]
|
|
api_url = sys.argv[2]
|
|
bearer_token = sys.argv[3]
|
|
send_raw_csv_as_json(filename, api_url, bearer_token)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|