How to set custom HTTP request headers?


#1

Hello!

I would like to set custom http headers on all requests coming from my app.

After doing some research I’ve found the protocol API with the method registerHttpProtocol but i’m a bit unsure how to use it.

Would it be possible to get an example?

Many thanks,
Adam

P.S: Protocol docs


#2

I am also curious about this. Did you ever find a solution?


#3

Hello,

Protocol is for an other things !
You must used WebRequest to modify/add/remove request headers HTTP
https://electron.atom.io/docs/api/web-request/#webrequestonbeforesendheadersfilter-listener

//Example

const {session} = require('electron')
    const filter = {
            urls: ["http://*/*", "https://*/*"]
        }
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
        details.requestHeaders['DNT'] = "1";
        details.requestHeaders["Referer"] = "https://mysite.fr";
        details.requestHeaders["Origin"] = "https://mysite.fr";
        callback({ cancel: false, requestHeaders: details.requestHeaders })
    })