简体   繁体   English

使用超级代理发布请求

[英]post request using superagent

I have been trying to make a post request to the pokeapi using superagent request lib.我一直在尝试使用超级代理请求库向 pokeapi 发出发布请求。 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'

Method is incorrect for the requested URL.请求的 URL 的方法不正确。 Use GET instead of POST .使用GET而不是POST

A sample GET request using superagent :使用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
  });

To know more, visit: superagent想了解更多,请访问: superagent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM