Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[nodejs] iterate through fetch response headers

https://github.github.io/fetch/#Headers

        const response = await fetch("https://example.com/api")

        for (const [key, value] of response.headers) {
            console.log(key, value)
          }

An alternative would be forEach()

        response.headers.forEach((value, key) => {
            console.log(value, key)
        })

Or using the entries() iterator (ES8)

          const headerIterator = response.headers.entries()
          console.log(headerIterator.next().value)
          console.log(headerIterator.next().value)

To add a new header just use set()

response.set(key, value)

Leave a Reply

Your email address will not be published. Required fields are marked *