Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In electron how to send custom header and value for every request?

Tags:

electron

I use electronjs for building a cross platform desktop application. I would like to send a custom header with a value for every request from electron. Initially in loadURL(), i could use extraHeaders to set the custom header. How to send it in all subsequent requests?

like image 926
jesus_the_coder Avatar asked Sep 06 '25 02:09

jesus_the_coder


1 Answers

As recommended by the documentation, you should use session object and the method 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 })
})
like image 76
Danizavtz Avatar answered Sep 07 '25 23:09

Danizavtz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!