我有一个我放在一起的小脚本。脚本执行以下操作:
-
在数组中定义多个变量
-
将这些值传递给API
-
API应该返回一个访问令牌
const fetch = require('node-fetch'); var orgInfo = { client_id: 'idgoeshere', client_secret: 'secretgoeshere', username: 'usernamegoeshere', password: 'passwordgoeshere', grant_type: 'granttypegoeshere' }; fetch('https://urlgoeshere', { method: "GET", body: JSON.stringify(orgInfo), headers: { "Content-Type": "application/json" }, credentials: "include" }).then(function(response) { response.access_token response.bearer response.expires_in response.scope return repsonse.text() }, function(error) { error.message }) console.log(orgInfo); console.log(response.access_token);
当我记录组织信息时,我得到以下输出:
{ client_id: 'idgoeshere',
client_secret: 'secretgoeshere',
username: 'usernamegoeshere',
password: 'passwordgoeshere',
grant_type: 'granttypegoeshere' }
当我尝试记录响应时。access_token,我得到一个ReferenceError: response is not defined
我的问题是:
- 需要定义响应吗?很明显,我被吼了,因为它不是。
- 是否有一种方法可以看到我是否从API自动获得任何东西?
我不是在找一个人给我一个答案,而是我只是在寻找一个正确的方向。那就太棒了。
感谢
所以这就是我所拥有的:
const fetch = require('node-fetch');
const orgInfo = {
client_id: ' ',
client_secret: ' ',
username: ' ',
password: ' ',
grant_type: ' '
};
(async() => {
const response = await fetch('https:// ', {
method: "GET",
body: JSON.stringify(orgInfo),
headers: {
"Content-Type": "application/json"
}
});
const data = await response.json();
console.log(data)
})
运行时不返回错误,但也不返回data的值
return response .text()应该是---->返回response.text ()
根据Fetch Documentation
- " Fetch API的Response接口表示对a的响应请求。你使用Response.Response()构造函数创建一个新的Response对象,但是您更有可能遇到作为另一个API操作的结果返回的Response对象—例如一个service worker。respondWith,或简单的fetch()。
对于您的问题"是否有一种方法可以查看我是否自动从API获得任何信息?">
-
您可以尝试使用console.log(response.status);它会给你请求的状态码。这些代码可以在这里找到。这里有一个使用这个的例子。
-
如果可以的话,我强烈建议尝试使用Postman或Thunder-client,它们简化了所有这些,并提供了您需要了解的有关响应的所有信息。测试API调用并确切地知道发生了什么是非常有用的。您还可以自动查看以其他语言编写的呼叫。
fetch
返回Promise
对象。
APromise
表示异步操作的最终完成(或失败)及其结果值。这意味着response.access_token
只保证在.then
块内有一个值(如果有的话),因为response
只有在承诺完成时才被评估。
你在控制台上什么也得不到的原因是,当你试图访问access_token
时,它不保证有一个值(因此console.log
没有输出-有没有输出)。
要解决这个问题,您需要在保证有响应时访问access_token
属性。
这是在承诺已经实现之后,所以要么:
- 将
console.log(response.access_token);
移动到.then
子句
或者更干净,更现代的解决方案是:
- 使用
await
(等效语法糖)
注意:Response
对象是整个HTTP响应的表示。
您使用的response.text()
将解析响应体作为字符串,而不是具有属性的JS对象。
我假设你想从Response
对象解析主体内容作为JSON到JS对象。在这种情况下,使用json()
方法,它将返回第二个承诺,解析从响应体解析中获得的JavaScript对象。
结果应该具有您想要的access_token
属性(考虑到API端点返回它)。
这个应该可以工作:
const response = await fetch('https://urlgoeshere', {
method: "GET",
body: JSON.stringify(orgInfo),
headers: {
"Content-Type": "application/json"
};
const data = await response.json();
console.log(data.access_token);
console.log(data.bearer);
console.log(data.expires_in);
console.log(data.scope);
...
const fetch = require('node-fetch');
var orgInfo = {
client_id: 'idgoeshere',
client_secret: 'secretgoeshere',
username: 'usernamegoeshere',
password: 'passwordgoeshere',
grant_type: 'granttypegoeshere'
};
fetch('https://urlgoeshere', {
method: "GET",
body: JSON.stringify(orgInfo),
headers: {
"Content-Type": "application/json"
},
credentials: "include"
}).then(function(response) {
response.access_token
response.bearer
response.expires_in
response.scope
console.log(response.access_token);
return repsonse.text()
}, function(error) {
error.message
})
console.log(orgInfo);
response在then方法调用的函数内,所以只能在这个函数
中访问。