arantes555 / electron-fetch

A light-weight module that brings window.fetch to the background process of Electron
Other
133 stars 21 forks source link

[solved] Get request headers before fetch? #51

Closed gallak-openfin closed 1 year ago

gallak-openfin commented 1 year ago

Is there a way to get the default request headers that are set before it is sent?

const res = await fetch(url, opts);

// something like
const requestHeaders = res.request.headers;

My team is piloting switching to this library over electron net lib and I wanted to compare our request headers with this library's request headers. Thanks.

arantes555 commented 1 year ago

Hello @gallak-openfin .

No sorry, I don't think there is a Response.headers on the Fetch specification, and this library tries to mimic it.

However, the example you gave would not show headers before they are sent, only after, as it is done after the await fetch, no ? Or did I misunderstand something?

You may be able to use the Network tab of the dev tools while debugging the main process ( https://www.electronjs.org/docs/latest/tutorial/application-debugging#main-process ), but to be honest it's something I have never tried, and I have no idea if it would work.

I am closing this issue, but do not hesitate to still post a response here if you try it, whether it works or no.

arantes555 commented 1 year ago

(I meant "I don't think there is a Response.request.headers", sorry for the typo)

gallak-openfin commented 1 year ago

@arantes555 Heh sorry I wasn't clear, for my investigation I just need to print Request headers. But I just realized I can do it the same way for both electron-fetch and the native electron net module:

if (opts?.session) {
      opts.session.webRequest.onBeforeSendHeaders((details, callback) => {
          const reqHeaders = Object.entries(details.requestHeaders)
              .map(([key, value]) => `${key}: ${value}`)
              .join('; ');

          console.log(`[fetch] Before send headers ${url}\nRequest Headers: ${reqHeaders}`);
          callback({ requestHeaders: details.requestHeaders });
      });
  }
}

It does look like the request headers are identical, here is an example for a favicon fetch

electron-fetch

Request Headers: sec-ch-ua: "Not=A?Brand";v="99", "Chromium";v="118"; sec-ch-ua-mobile: ?0; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.0 OpenFin/35.118.78.29 Safari/537.36; sec-ch-ua-platform: "Windows"; Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8; Sec-Fetch-Site: same-origin; Sec-Fetch-Mode: no-cors; Sec-Fetch-Dest: image; Referer: http://localhost:3000/; Accept-Encoding: gzip, deflate, br, zstd; Accept-Language: en-US,en;q=0.9

net.request

Request Headers: sec-ch-ua: "Not=A?Brand";v="99", "Chromium";v="118"; sec-ch-ua-mobile: ?0; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.0 OpenFin/35.118.78.29 Safari/537.36; sec-ch-ua-platform: "Windows"; Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8; Sec-Fetch-Site: same-origin; Sec-Fetch-Mode: no-cors; Sec-Fetch-Dest: image; Referer: http://localhost:3000/; Accept-Encoding: gzip, deflate, br, zstd; Accept-Language: en-US,en;q=0.9

Hopefully this helps someone else.