post 使用超级代理请求
post request using superagent
我一直在尝试使用 superagent 请求库向 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'
请求的方法不正确URL。使用 GET
而不是 POST
.
示例 GET
请求使用 superagent
:
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
我一直在尝试使用 superagent 请求库向 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'
请求的方法不正确URL。使用 GET
而不是 POST
.
示例 GET
请求使用 superagent
:
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