以下功能在我的 Electron 應用程式中的 Angular 組件中運行:
getData() {
const url = 'https://reqres.in/api/users?page=1';
this.http.get<any>(url).toPromise()
.then(response=> {
//...
alert(JSON.stringify(response));
});
}
如果您瀏覽到https://reqres.in/api/users?page=1,警報會列印出與您將看到的相同的文本
IE:
{"page":1,"per_page":6,"total":12,"total_pages":2,"data":[{"id":1,"email":"[email protected]","first_name":"George","last_name":"Bluth","avatar":"https://reqres.in/img/faces/1-image.jpg"},{"id":2,"email":"[email protected]","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},{"id":3,"email":"[email protected]","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"},{"id":4,"email":"[email protected]","first_name":"Eve","last_name":"Holt","avatar":"https://reqres.in/img/faces/4-image.jpg"},{"id":5,"email":"[email protected]","first_name":"Charles","last_name":"Morris","avatar":"https://reqres.in/img/faces/5-image.jpg"},{"id":6,"email":"[email protected]","first_name":"Tracey","last_name":"Ramos","avatar":"https://reqres.in/img/faces/6-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
現在,我嘗試了幾種方法來讓它在 Electron 主應用程式端作業。
這是我目前的化身(它在我的 main.ts 的 app.on 函式中):
const { net } = require('electron')
const request = net.request({
method: 'GET',
url: 'https://reqres.in/api/users?page=1'})
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()
這一切都運行沒有錯誤,我有 response.statusCode = 200。
但是在回應中哪里可以找到從https://reqres.in/api/users?page=1回傳的 JSON 資料?
我認為它可能在“塊”變數中,如果我對它進行字串化,我有:
'{“型別”:“緩沖區”,“資料”:[123,34,112,97,103,101,34,58,49,44,34,112,101,114,95,112,97,103,101,34,58,54,44,34,116,111,116,97,108,34,58 ,49,50,44,34,116,111,116,97,108,95,112,97,103,101,115,34,58,50,44,34,100,97,116,97,34,58,91,123,34,105,100,34,58,49,944,34,50,101, ,58,34,103,101,111,114,103,101,46,98,108,117,116,104,64,114,101,113,114,101,115,46,105,110,34,44,34,102,105,114,115,116,95,110,97,109,101,34,58,34,71,101,111,114,103,101,34,44,34,108,97,115,116,95,110,97,109,101,34,58,34,66 ,1 ... 117,112,112,111,114,116,34,58,123,34,117,114,108,34,58,34,104,116,116,112,115,58,47,47,114,101,113,114,101,115,46,105,110,47,35,115,117,112,112,111,114,116,45,104,101,97,100,105,110,103,34,44,34,116,101,120,116,34,58,34,84,111,32,107,101,101,112,32 ,82,101,113,82,101,115,32,102,114,101,101,44,32,99,111,110,116,114,105,98,117,116,105,111,110,115,32,116,111,119,97,114,100,115,32,115,101,114,118,101,114,32,99,111,115,116,115,32,97,114,101,32,97,112,112,114,101,99,105,97,116,101,100,33,34,125,125]}”
這是二進制形式嗎?如果是這樣我將如何轉換它?
還是我應該尋找與使用 net.request 完全不同的方法?
或者實際上,我可以像在 Angular 組件中那樣使用電子應用程式 main.ts 中的 HttpClient 嗎?
我問因為在 Angular 組件中我通過建構式引入它,如下所示:
constructor(
private http:HttpClient,) {
super();
但是 Electron 應用程式的 main.ts 中沒有建構式。
非常感謝您的幫助。
uj5u.com熱心網友回復:
在您的代碼中
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`)
})
chunk是部分回應體資料的Buffer。為了獲得 JSON 格式的完整回應正文,您需要連接所有緩沖區,然后將其決議為 JSON。IE
let buffers = [];
response.on('data', (chunk) => {
console.log(`Buffer: ${chunk}`);
buffers.push(chunk);
})
response.on('end', () => {
let responseBodyBuffer = Buffer.concat(buffers);
let responseBodyJSON = JSON.parse(responseBodyBuffer.toString());
console.log(`BODY: ${responseBodyJSON}`);
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/431282.html
