遇到一个问题,刚接触axios,什么代码会从这种API接收数据?因为我试图做某事,这里有一个例子(但没有任何效果,啊哈):
const axios = require('axios');
async function testaxios() {
let res = await axios.get('https://streamcraft.net/api/forum/populars');
let test = res.data.login;
console.log(`test: ${test}`)
}
testaxios();
响应结构(您需要登录才能进行测试):
{"populars":[{"login":"Notice","uuid":"ed3977c3-1e69-3b9b-19f3-83041c6b71f7","reputation":733},{"login":"Aleksey3275","uuid":"bb06a6e0-43fd-11ea-977d-5557488d6d86","reputation":624},{"login":"Mine03","uuid":"923680ce-cb06-e915-3226-6ab89f650691","reputation":617},{"login":"SmartLight","uuid":"254d94d0-02e2-11e9-a7c0-15ed89e3b000","reputation":602},{"login":"GizzMOO","uuid":"2e2d7650-8eea-11ea-b517-7fbff509f2eb","reputation":533},{"login":"NeoD","uuid":"c8dbe345-f2c2-0781-6f43-4cc3cfe0323f","reputation":321},{"login":"xchatos","uuid":"837805a0-5301-11e9-8aa4-47ea0e328458","reputation":308}]}
错误:
(node:38220) UnhandledPromiseRejectionWarning: Error: Request failed with status code 403
at createError (D:\User.Data\Desktop\vbtg\node_modules\axios\lib\core\createError.js:16:15)
at settle (D:\User.Data\Desktop\vbtg\node_modules\axios\lib\core\settle.js:17:12)
at IncomingMessage.handleStreamEnd (D:\User.Data\Desktop\vbtg\node_modules\axios\lib\adapters\http.js:260:11)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:38220) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To
terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:38220) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
很可能,参数根本不正确,因为到目前为止我已经从我的机器上运行了测试,所以肯定可以访问。需要更改哪些内容才能使代码正常工作?