2022-05-17 乐帮网
javascript electron
electron-fetch是将 window.fetch 引入 Electron 后台进程的轻量级模块。 它主要继承自node-fetch模块。主要使用过程
首先安装:
$ npm install electron-fetch --save
示例:
import fetch from 'electron-fetch'
// or
// const fetch = require('electron-fetch').default
// plain text or html
fetch('https://github.com/')
.then(res => res.text())
.then(body => console.log(body))
// json
fetch('https://api.github.com/users/github')
.then(res => res.json())
.then(json => console.log(json))
// catching network error
// 3xx-5xx responses are NOT network errors, and should be handled in then()
// you only need one catch() at the end of your promise chain
fetch('http://domain.invalid/')
.catch(err => console.error(err))
// stream
// the node.js way is to use stream when possible
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
.then(res => {
const dest = fs.createWriteStream('./octocat.png')
res.body.pipe(dest)
})
// buffer
// if you prefer to cache binary data in full, use buffer()
// note that buffer() is a electron-fetch only API
import fileType from 'file-type'
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
.then(res => res.buffer())
.then(buffer => fileType(buffer))
.then(type => { /* ... */ })
// meta
fetch('https://github.com/')
.then(res => {
console.log(res.ok)
console.log(res.status)
console.log(res.statusText)
console.log(res.headers.raw())
console.log(res.headers.get('content-type'))
})
// post
fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })
.then(res => res.json())
.then(json => console.log(json))
// post with stream from file
import { createReadStream } from 'fs'
const stream = createReadStream('input.txt')
fetch('http://httpbin.org/post', { method: 'POST', body: stream })
.then(res => res.json())
.then(json => console.log(json))
// post with JSON
const body = { a: 1 }
fetch('http://httpbin.org/post', {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json))
// post with form-data (detect multipart)
import FormData from 'form-data'
const form = new FormData()
form.append('a', 1)
fetch('http://httpbin.org/post', { method: 'POST', body: form })
.then(res => res.json())
.then(json => console.log(json))
// post with form-data (custom headers)
// note that getHeaders() is non-standard API
import FormData from 'form-data'
const form = new FormData()
form.append('a', 1)
fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
.then(res => res.json())
.then(json => console.log(json))
// node 7+ with async function
(async function () {
const res = await fetch('https://api.github.com/users/github')
const json = await res.json()
console.log(json)
})()
// providing proxy credentials (electron-specific)
fetch(url, {
onLogin (authInfo) { // this 'authInfo' is the one received by the 'login' event. See https://www.electronjs.org/docs/latest/api/client-request#event-login
return Promise.resolve({ username: 'testuser', password: 'testpassword' })
}
})
关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com
庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。
如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力