undici node make api request with header for Authorization

1.8k Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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;