node.jsapirequestundici

undici node make api request with header for Authorization


im trying to make a api request with undici

const apiRequest = await request("https://example.com")
const apiJson = await apiRequest.body.json()
const info = apiJson.data

which works so far... but with another API that I want to use, verification is done via the header.

Authorization: xxxxxx-xxxxxx-xxxxxx

but I can't find in the docs exactly how it works with undici


Solution

  • The authorization header can be added to the request as follows:

    const apiRequest = await request('https://example.com', {
      headers: { Authorization: 'xxxxxx-xxxxxx-xxxxxx' },
    });
    const apiJson = await apiRequest.body.json();
    const info = apiJson.data;