Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using system proxy in browser and renderer process

In my electron app, I have coded a RestClient which is executed in main and renderer process. The first request, done in the main process, is done with the net library from electron and it successfully uses the system proxy settings.

The next request, executed in renderer process cannot use net library, because this belongs to main only. Therefore I switch to normal https request, but this does not use system proxy settings.

Is there a way to use the system proxy in the renderer process?

Edit: Maybe a more general question: What is the best practise in electron to make web requests? Is there some standard way to use http/https, request, net or fetch? Which way would use the system proxy?

like image 397
KevinZ01 Avatar asked Jul 10 '19 14:07

KevinZ01


1 Answers

i tested this on windows and it runs smoothly with fetch() in the renderer request with and without system proxy


but i think best practice at this point is to send a message from renderer process via ipcRenderer to the main process.

so in your renderer process send a message with

const ipc = require('electron').ipcRenderer;
ipc.send('hello','a string', 10);

and in your main process receive the message and make your api request like the first

ipc.on('fromMain', (event, messages) => {
    // do your api request and send data back
}

after this send the data back to the renderer process

why?
Advantages: strict separation of frontend and backend, only one method for API query and of course "don't repeat yourself"

like image 162
Tobias Avatar answered Nov 10 '22 00:11

Tobias