下面的函数使用HttpClient从角分量(在电子应用程序中)工作:
var auth = "Bearer" + "abdedede";
let header = new HttpHeaders({ "Content-Type": 'application/json', "Authorization": auth});
const requestOptions = {headers: header};
const url = 'https://reqres.in/api/users?page=1';
this.http.get<any>(url, requestOptions).toPromise()
.then(response=> {
//...
alert(JSON.stringify(response));
});
}现在,这里有一个来自电子端的调用,它调用相同的端点,但头中没有授权和内容类型:
let buffers:any = [];
const { net } = require('electron')
const request = net.request({
method: 'GET',
url: 'https://reqres.in/api/users?page=1'})
request.on('response', (response) => {
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
response.on('data', (chunk) => {
buffers.push(chunk);
})
response.on('end', () => {
let responseBodyBuffer = Buffer.concat(buffers);
let responseBodyJSON = responseBodyBuffer.toString();
responseBodyJSON = responseBodyJSON;
})
})
request.end()(后一种功能是由于在这里回复的海报:In an Electron Application I am successfully making an HTTP GET request from an Angular component. How can I do the same thing from the Electron side?)。
我的问题是,请有人建议\告诉我如何在这个调用中添加授权和内容类型标头信息,这样它就可以复制角形版本的功能--即在GET调用中传递requestOptions数据?
谢谢。
发布于 2022-02-22 15:29:21
我已经找到了。我需要补充一句:
Request.setHeader(“内容类型”,“应用程序/json”);request.setHeader(“授权”,auth);
在我打电话之前:
Request.on(“响应”,(响应) => {
https://stackoverflow.com/questions/71223532
复制相似问题