Skip to content

Instantly share code, notes, and snippets.

@UnleashTheCode
Created June 25, 2024 10:56
Show Gist options
  • Save UnleashTheCode/0d2fd30316f3357aaa34f01f804fb0ab to your computer and use it in GitHub Desktop.
Save UnleashTheCode/0d2fd30316f3357aaa34f01f804fb0ab to your computer and use it in GitHub Desktop.
import requests
def send_http_request_from_file(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
# Parse the first line to get the HTTP method and URL
request_line = lines[0].strip()
method, url = request_line.split(' ', 1)
headers = {}
body = None
is_body = False
for line in lines[1:]:
line = line.strip()
if line == '':
is_body = True
continue
if is_body:
if body is None:
body = line
else:
body += '\n' + line
else:
header_name, header_value = line.split(': ', 1)
headers[header_name] = header_value
response = requests.request(method, url, headers=headers, data=body)
return response
# Example usage
file_path = 'path_to_your_file.txt'
response = send_http_request_from_file(file_path)
print(f'Status Code: {response.status_code}')
print(f'Response Body: {response.text}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment