简体   繁体   中英

How to send request from Express server to another server?

I am trying to fetch data from Express server from another external server.

That is search api, which is post request and I am from client get body params.

Then try to send that params on another server: http://search-api.com/d/explore?

public static async search(req: Express.Request, res: Express.Response) {
    try {
      const data = await axios.post(
        `http://search-api.com/d/explore?user_id=${
          req.body.id
        }`,
        {
          query: req.body.query,
          topics: req.body.topics
        }
      );

      res.send(data);
    } catch (err) {
      ErrorHandler.handle('Error fetching search response.', err, res);
    }
  }

This is response Error:

[ERROR] default - "Error fetching search response." Error: Request failed with status code 422

I was check params exist and they are ok.

Does anyone have idea what can be the problem here?

UPDATED :

This is how body of request should looks: 在此处输入图片说明

Probably you have forgot about http:// protocol on beginning; also, you have wwww instead of www , but I assume that it's just dummy text there:

public static async search(req: Express.Request, res: Express.Response) {
try {
  const data = await axios.post(
    `http://www.search-api.com/d/explore?user_id=${
      req.body.id
    }`,
    {
      query: req.body.query,
      topics: req.body.topics
    }
  );

  res.send(data);
 } catch (err) {
   ErrorHandler.handle('Error fetching search response.', err, res);
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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