0

I am getting response from request.post but I want the body to send outside of request.post(), how do I do that?

request.post(options, (err, resp, body) => {
      if (err) {
          return console.log(err);
      }
      console.log(JSON.parse(body));
  });
Nadish
  • 89
  • 1
  • 1
  • 9
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Luca Kiebel Jan 30 '22 at 16:21

1 Answers1

0

This seems like you're looking for a controller-service structure, where the controller lists the endpoints and calls the service functions.

This is a very much simplified version, but you could do something like this:

let serviceFunction = (err, resp, body) => {
    if (err) {
          return console.log(err);
    }
    console.log(JSON.parse(body));
}

request.post(options, serviceFunction);

This way you separate the endpoint definition and the handling of the request.