1. import requests
  2. def strip_metadata_headers(url):
  3. """
  4. Makes a request to the given URL and strips metadata headers
  5. (e.g., Cache-Control, Connection, Transfer-Encoding) from the response.
  6. Args:
  7. url (str): The URL to make the request to.
  8. Returns:
  9. requests.Response: The response object with metadata headers removed.
  10. Returns None if an error occurs.
  11. """
  12. try:
  13. response = requests.get(url, allow_redirects=True) #Make the request
  14. # Create a copy of the headers to modify
  15. stripped_headers = dict(response.headers)
  16. #Remove metadata headers
  17. metadata_headers = ['Cache-Control', 'Connection', 'Transfer-Encoding']
  18. for header in metadata_headers:
  19. if header in stripped_headers:
  20. del stripped_headers[header]
  21. #Set the stripped headers on the response
  22. response.headers = stripped_headers
  23. return response
  24. except requests.exceptions.RequestException as e:
  25. print(f"An error occurred: {e}")
  26. return None
  27. if __name__ == '__main__':
  28. #Example Usage:
  29. test_url = "https://www.example.com"
  30. response = strip_metadata_headers(test_url)
  31. if response:
  32. print(response.status_code)
  33. print(response.headers)

Add your comment