Reputation: 55
I have been trying to make a post request to the pokeapi using superagent request lib. I am not sure why the request is not successful. Below is my code. I am fairly new to superagent, so any advice will be appreciated.
try {
const pokemon = await superagent.post(
`https://pokeapi.co/api/v2/pokemon/${req.body.id}`
);
console.log(pokemon);
} catch (err) {
console.error(err);
}
next();
The error listed was:
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'
Upvotes: 1
Views: 2338
Reputation: 2479
Method is incorrect for the requested URL. Use GET
instead of POST
.
A sample GET
request using 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
});
To know more, visit: superagent
Upvotes: 1