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?
Asked
Active
Viewed 3,833 times
4
-
What lib do you use to make the requests? – Danizavtz Nov 22 '20 at 11:58
-
I use electron's webRequest object. _i.e. webRequest = window.webContents.session.webRequest;_ Do you recommend using any lib ? – jesus_the_coder Nov 22 '20 at 12:30
1 Answers
5
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 })
})

Danizavtz
- 3,166
- 4
- 24
- 25
-
-
hello @EmmanuelMurairi, without code is very dificult see why it is not working. – Danizavtz Jul 17 '23 at 14:47