How can I make an HTTP request from within Node.js or Express.js? I need to connect to another service. I am hoping the call is asynchronous and that the callback contains the remote server’s response.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is a snippet of some code from a sample of mine. It’s asynchronous and returns a JSON object. It can do any form of GET request.
Note that there are more optimal ways (just a sample) – for example, instead of concatenating the chunks you put into an array and join it etc… Hopefully, it gets you started in the right direction:
It’s called by creating an options object like:
And providing a callback function.
For example, in a service, I require the REST module above and then do this:
UPDATE
If you’re looking for
async/await(linear, no callback), promises, compile time support and intellisense, we created a lightweight HTTP and REST client that fits that bill:Microsoft typed-rest-client