在electron中,如何为每个请求发送自定义标头和值?

在electron中,如何为每个请求发送自定义标头和值?,electron,Electron,我使用electronjs构建一个跨平台的桌面应用程序。我想发送一个自定义标题,其中包含来自electron的每个请求的值。最初在loadURL()中,我可以使用extraHeaders来设置自定义头。如何在所有后续请求中发送它?根据的建议,您应该使用会话对象和方法onBeforeSendHeaders: const { session } = require('electron') // Modify the user agent for all requests to the follow

我使用electronjs构建一个跨平台的桌面应用程序。我想发送一个自定义标题,其中包含来自electron的每个请求的值。最初在loadURL()中,我可以使用extraHeaders来设置自定义头。如何在所有后续请求中发送它?

根据的建议,您应该使用
会话
对象和方法
onBeforeSendHeaders

const { session } = require('electron')

// Modify the user agent for all requests to the following urls.
const filter = {
  urls: ['https://*.github.com/*', '*://electron.github.io']
}

session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
  details.requestHeaders['User-Agent'] = 'MyAgent'
  callback({ requestHeaders: details.requestHeaders })
})

您使用什么库发出请求?我使用electron的webRequest对象。i、 e.webRequest=window.webContents.session.webRequest;你建议使用任何库吗?