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)

[JavaScript] Download base64 encoded file within a browser

            const sBase64 = "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3....."
            const arrayBuffer = new Uint8Array([...window.atob(sBase64)].map(char => char.charCodeAt(0)))
            const fileLink = document.createElement('a')

            fileLink.href = window.URL.createObjectURL(new Blob([arrayBuffer]))
            fileLink.setAttribute('download', "example.pdf")
            document.body.appendChild(fileLink)
            fileLink.click()

Or use the npm package FileSaver.

import { saveAs } from "file-saver";

const sBase64 = "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3....."
const buffer = Buffer.from(sBase64, "base64") //Buffer is only available when using nodejs
saveAs(new Blob([buffer]), "example.pdf")