Skip to content

Instantly share code, notes, and snippets.

@alexewerlof
Last active February 22, 2023 16:59
Show Gist options
  • Select an option

  • Save alexewerlof/fedfe80d59aa1c096267600595ba423e to your computer and use it in GitHub Desktop.

Select an option

Save alexewerlof/fedfe80d59aa1c096267600595ba423e to your computer and use it in GitHub Desktop.
converting fetch response headers to a plain javascript object
const fetch = require('node-fetch')
function responseHeadersAsObject(response) {
const headers = {}
const keyVals = [...response.headers.entries()]
keyVals.forEach(([key, val]) => {
console.log(`${key}: ${val}`)
headers[key] = val
})
return headers
}
async function main() {
const url = 'https://www.aol.com'
const response = await fetch(url)
const responseHeaders = responseHeadersAsObject(response)
return responseHeaders
}
main().then(console.log, console.error)
@alexkonst
Copy link

@alexewerlof
Copy link
Author

thanks @alexkonst. updated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment