function logResponseHeaders(response) {
// Check if the response object is valid
if (!response || typeof response.headers !== 'object') {
console.error("Invalid response object or missing headers.");
return;
}
for (const headerName in response.headers) {
// Iterate through each header name
if (response.headers.hasOwnProperty(headerName)) {
const headerValue = response.headers[headerName];
console.log(`Header: ${headerName} = ${headerValue}`); // Log the header name and value
}
}
}
// Example usage (assuming you have a 'response' object)
// You would replace this with your actual response object
// For testing purposes:
function simulateResponse(headers) {
return {
headers: headers
};
}
// Example 1:
const response1 = simulateResponse({
'Content-Type': 'application/json',
'X-Custom-Header': 'somevalue'
});
logResponseHeaders(response1);
// Example 2: Empty headers
const response2 = simulateResponse({});
logResponseHeaders(response2);
// Example 3: Invalid response
logResponseHeaders(null);
Add your comment