所以我的应用程序有一个小问题-我需要向PHP后端发送GET请求,该请求使我可以将Amazon S3 url传递到我需要解析并在某些功能中使用的文件。我为此使用SuperAgent createRequest(),它工作得很好,我得到了JSON,当我向浏览器输入该URL时,我可以看到该JSON,但是我需要我的应用程序才能从URL中读取此JSON,而SuperAgent请求没有不要那样做。这是代码示例(简体):
async process(projectId: number, token: string): any {
const projectDocumentUrl = `${this.config.apiUrl}/v1/projects/${projectId}/files/script-document`;
const projectScriptRequest = createRequest(projectDocumentUrl);
projectScriptRequest.set('Authorization', token).set('Content-type', 'application/json');
const source: Document = {};
let scriptLocation = '';
await projectScriptRequest.then((response: Response) => {
console.log(response.body.file.url);
scriptLocation = response.body.file.url;
});
const scriptJsonRequest = await createRequest(scriptLocation);
await scriptJsonRequest.then((scriptResponse: Response) => {
console.log(scriptResponse.body);
source.document = scriptResponse.body.source.document;
});
}
还有其他方法可从url获取JSON,还是我在这里做错了?我将不胜感激。
要在NodeJS中读取Json URL。您可以使用包node-fetch
进行读取。首先,您需要通过命令node-fetch
安装npm install node-fetch
。然后使用node-fetch
将请求发送到该URL。我相信这是一个GET请求。这是示例代码:
const fetch = require('node-fetch');
(async(){
const url = YOU_URL;
const response = await fetch(url);
const result = await response.toJson();
})();