import requests
def strip_metadata_headers(url):
"""
Makes a request to the given URL and strips metadata headers
(e.g., Cache-Control, Connection, Transfer-Encoding) from the response.
Args:
url (str): The URL to make the request to.
Returns:
requests.Response: The response object with metadata headers removed.
Returns None if an error occurs.
"""
try:
response = requests.get(url, allow_redirects=True) #Make the request
# Create a copy of the headers to modify
stripped_headers = dict(response.headers)
#Remove metadata headers
metadata_headers = ['Cache-Control', 'Connection', 'Transfer-Encoding']
for header in metadata_headers:
if header in stripped_headers:
del stripped_headers[header]
#Set the stripped headers on the response
response.headers = stripped_headers
return response
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
if __name__ == '__main__':
#Example Usage:
test_url = "https://www.example.com"
response = strip_metadata_headers(test_url)
if response:
print(response.status_code)
print(response.headers)
Add your comment