Skip to content

Instantly share code, notes, and snippets.

@aljorgevi
Last active July 30, 2024 13:19
Show Gist options
  • Save aljorgevi/868b7e6ed24685e07b5a992d05ada609 to your computer and use it in GitHub Desktop.
Save aljorgevi/868b7e6ed24685e07b5a992d05ada609 to your computer and use it in GitHub Desktop.
etc
import requests
import json
import csv
from io import StringIO
def read_csv(file_path):
with open(file_path, mode='r', encoding='utf-8') as file:
csv_reader = csv.reader(file)
csv_data = list(csv_reader)
return csv_data
def read_json(file_path):
with open(file_path, mode='r', encoding='utf-8') as file:
json_data = json.load(file)
return json_data
def send_post_request(csv_file, json_file, url):
csv_data = read_csv(csv_file)
json_data = read_json(json_file)
# Convert CSV data to string
csv_string = StringIO()
csv_writer = csv.writer(csv_string)
csv_writer.writerows(csv_data)
csv_string = csv_string.getvalue()
pip install requests
# Prepare the payload
files = {
'data': (None, csv_string, 'text/csv'),
'request': (None, json.dumps(json_data), 'application/json')
}
response = requests.post(url, files=files)
return response
if __name__ == "__main__":
csv_file_path = 'path/to/your/file.csv'
json_file_path = 'path/to/your/file.json'
post_url = 'http://your-java-backend-endpoint'
response = send_post_request(csv_file_path, json_file_path, post_url)
print(response.status_code)
print(response.text)
@aljorgevi
Copy link
Author

Screenshot 2023-11-07 at 13 04 47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment