Created
June 25, 2024 10:56
-
-
Save UnleashTheCode/0d2fd30316f3357aaa34f01f804fb0ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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