Skip to content

How to customize http headers in electron

Posted on:May 18, 2022

Table of contents

Open Table of contents

1. request in node directly

A http client like make-fetch-happen or node-fetch will help you get what you want.

2. intercept request headers before send

in main process, introduce the following code:

// win is the BrowserWindow desired 
const session = win.webContents.session

session.webRequest.onBeforeSendHeaders((details, callback) => {
    const needIntercept = /*...*/

    if (!needIntercept) {
      callback({
        requestHeaders: details.requestHeaders,
      })
    } else {
      callback({
        requestHeaders: {
          ...yourCustomHeaders,
          ...details.requestHeaders,
        },
      })
    }
  })