我一直在尝试使用superagent request lib向pokeapi发出post请求。我不确定请求不成功的原因。下面是我的代码。我是superagent的新手,因此任何建议都将不胜感激。
try {
const pokemon = await superagent.post(
`https://pokeapi.co/api/v2/pokemon/${req.body.id}`
);
console.log(pokemon);
} catch (err) {
console.error(err);
}
next();列出的错误为:
error: Error: cannot POST /api/v2/pokemon/1 (404)
at Response.toError (G:\CodersArts\pokemon-app\pokemonapi\node_modules\superagent\lib\node\response.js:98:13)
at ResponseBase._setStatusProperties (G:\CodersArts\pokemon-app\pokemonapi\node_modules\superagent\lib\response-base.js:119:48)
at new Response (G:\CodersArts\pokemon-app\pokemonapi\node_modules\superagent\lib\node\response.js:44:8)
at Request._emitResponse (G:\CodersArts\pokemon-app\pokemonapi\node_modules\superagent\lib\node\index.js:930:18)
at IncomingMessage.<anonymous> (G:\CodersArts\pokemon-app\pokemonapi\node_modules\superagent\lib\node\index.js:1127:42)
at Stream.emit (events.js:315:20)
at Unzip.<anonymous> (G:\CodersArts\pokemon-app\pokemonapi\node_modules\superagent\lib\node\unzip.js:53:12)
at Unzip.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
status: 404,
text: '<!DOCTYPE html>\n' +
'<html lang="en">\n' +
'<head>\n' +
'<meta charset="utf-8">\n' +
'<title>Error</title>\n' +
'</head>\n' +
'<body>\n' +
'<pre>Cannot POST /api/v2/pokemon/1</pre>\n' +
'</body>\n' +
'</html>\n',
method: 'POST',
path: '/api/v2/pokemon/1'发布于 2021-04-08 03:29:07
方法对于请求的URL不正确。使用GET而不是POST。
使用superagent的示例GET请求
const nocache = require('superagent-no-cache');
const superagent = require('superagent');
const prefix = require('superagent-prefix')('/static');
superagent
.get('/some-url')
.query({ action: 'edit', city: 'London' }) // query string
.use(prefix) // Prefixes *only* this request
.use(nocache) // Prevents caching of *only* this request
.end((err, res) => {
// Do something
});要了解更多信息,请访问:superagent
https://stackoverflow.com/questions/66992573
复制相似问题