clarify : how to pass arguments to new Headers.append .set #1427
-
i want to construct headers for fetch (v3.1) all goes well if i pass an obj import { Headers } from 'node-fetch'
const headers = new Headers()
const headers_default = [ 'referer:somesite.com', 'key1:value1' ]
headers.append(headers_defaults) Expected behavior, if applicable |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
The Headers constructor takes either a 2D array or an object, not a flat array... new Headers([
['referer', 'somesite.com'],
['content-type': 'text/plain'],
['key1', 'value1']
]) |
Beta Was this translation helpful? Give feedback.
-
Sorry, asking for one more help! const headers = new Headers()
let headers_defaults = [
['User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0'],
['referer', 'somesite.com'],
]
headers.append(headers_defaults) give me
|
Beta Was this translation helpful? Give feedback.
-
import fetch from 'node-fetch';
import { Headers } from 'node-fetch';
let headers_defaults = [
['User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0'],
['Referer', 'somesite.com'],
]
headers_defaults.forEach(pair => headers.append(...pair)) still give me
TypeError: Failed to construct 'Headers': The provided value is not of type '(sequence<sequence> or record<ByteString, ByteString>) UPD! :merci: all goes well. the error above for the visibility scope problem in my func so fixed it! When placed in inner func all rocks! 👍 export const Downloader = async (url, buffsize, headers) => {
headers = new Headers()
headers_defaults.forEach(pair => headers.append(...pair)) // works as expected!
/// ... skip the rest
} |
Beta Was this translation helpful? Give feedback.
The Headers constructor takes either a 2D array or an object, not a flat array...