1. function logResponseHeaders(response) {
  2. // Check if the response object is valid
  3. if (!response || typeof response.headers !== 'object') {
  4. console.error("Invalid response object or missing headers.");
  5. return;
  6. }
  7. for (const headerName in response.headers) {
  8. // Iterate through each header name
  9. if (response.headers.hasOwnProperty(headerName)) {
  10. const headerValue = response.headers[headerName];
  11. console.log(`Header: ${headerName} = ${headerValue}`); // Log the header name and value
  12. }
  13. }
  14. }
  15. // Example usage (assuming you have a 'response' object)
  16. // You would replace this with your actual response object
  17. // For testing purposes:
  18. function simulateResponse(headers) {
  19. return {
  20. headers: headers
  21. };
  22. }
  23. // Example 1:
  24. const response1 = simulateResponse({
  25. 'Content-Type': 'application/json',
  26. 'X-Custom-Header': 'somevalue'
  27. });
  28. logResponseHeaders(response1);
  29. // Example 2: Empty headers
  30. const response2 = simulateResponse({});
  31. logResponseHeaders(response2);
  32. // Example 3: Invalid response
  33. logResponseHeaders(null);

Add your comment