0

I'm trying to make my electron application send a request to a website, presumably on the renderer thread. I have tried to following code stated on the docs itself and I seem to encounter the following error: Cannot read property 'request' of undefined

This is the code:

  const { app } = require('electron')
  const { net } = require('electron')
  const request = net.request('https://github.com')
  request.on('response', (response) => {
    console.log(`STATUS: ${response.statusCode}`)
    console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
    response.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`)
    })
    response.on('end', () => {
      console.log('No more data in response.')
    })
  })
  request.end()
NexTre
  • 11
  • 1
  • 4
  • `app.whenReady().then(() => {`. you have missed this line. – Monty Tomar Jun 05 '21 at 15:50
  • `net` is a main process only API, you are trying to use it in the renderer process (index.html) this is not and will not be supported. In the renderer process use `fetch` instead – Mohamad Osama Jun 05 '21 at 15:51

1 Answers1

0

I had the same problem and I get exactly the same error message when I tried to use net from the renderer process so I assume you did the same. As a quote from electron GitHub issue page: https://github.com/electron/electron/issues/25384

net is the main process only API, you are trying to use it in the renderer process (index.html) this is not and will not be supported. In the renderer process use fetch instead

An example of usage of fetch:

fetch('https://github.com/')
                .then(res => res.text())
                .then(body => console.log(body))

Ps. You may also have a problem with Content Security Policy just like me. In this case, an other answer helps: Refused to load the script because it violates the following Content Security Policy directive: "style-src 'self' 'unsafe-inline'