Use the browser Fetch API from the main process in Electron
npm install electron-main-fetch> Use the browser Fetch API from the main process in Electron
``sh`
npm install electron-main-fetch
Requires Electron 30 or later.
`js
import fetch from 'electron-main-fetch';
const response = await fetch('https://api.ipify.org');
console.log(await response.text());
//=> '170.56.15.35'
`
Same options as Fetch
`js`
{
type: 'cors',
url: 'https://api.ipify.org/',
redirected: false,
status: 200,
ok: true,
statusText: 'OK',
bodyUsed: false,
headers: {
keys: [Function],
entries: [Function],
values: [Function],
get: [Function],
has: [Function],
set: [Function],
append: [Function],
delete: [Function]
},
clone: [Function],
arrayBuffer: [Function],
json: [Function],
text: [Function]
}
We don't have direct access to the body stream, so there is no body property. There are some methods that are not useful in Node.js, like formData(), redirect(), etc. Some other methods are not very cross-browser compatible, so they are less common to use.
Missing:
- bodyblob()
- formData()
- error()
-
A small difference is that our .clone()` method is async.
- electron-store - Save and load data like user settings, app state, cache, etc
- electron-debug - Adds useful debug features to your Electron app
- electron-context-menu - Context menu for your Electron app
- electron-dl - Simplified file downloads for your Electron app
- electron-unhandled - Catch unhandled errors and promise rejections in your Electron app