Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:11:52+00:00 2026-05-30T22:11:52+00:00

How can I make an HTTP request from within Node.js or Express.js? I need

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:11:53+00:00Added an answer on May 30, 2026 at 10:11 pm

    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:

    const http = require('http');
    const https = require('https');
    
    /**
     * getJSON:  RESTful GET request returning JSON object(s)
     * @param options: http options object
     * @param callback: callback to pass the results JSON object(s) back
     */
    
    module.exports.getJSON = (options, onResult) => {
      console.log('rest::getJSON');
      const port = options.port == 443 ? https : http;
    
      let output = '';
    
      const req = port.request(options, (res) => {
        console.log(`${options.host} : ${res.statusCode}`);
        res.setEncoding('utf8');
    
        res.on('data', (chunk) => {
          output += chunk;
        });
    
        res.on('end', () => {
          let obj = JSON.parse(output);
    
          onResult(res.statusCode, obj);
        });
      });
    
      req.on('error', (err) => {
        // res.send('error: ' + err.message);
      });
    
      req.end();
    };
    
    

    It’s called by creating an options object like:

    const options = {
      host: 'somesite.com',
      port: 443,
      path: '/some/path',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    };
    

    And providing a callback function.

    For example, in a service, I require the REST module above and then do this:

    rest.getJSON(options, (statusCode, result) => {
      // I could work with the resulting HTML/JSON here. I could also just return it
      console.log(`onResult: (${statusCode})\n\n${JSON.stringify(result)}`);
    
      res.statusCode = statusCode;
    
      res.send(result);
    });
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make an HTTP POST request from outside the browser, but the
can I make my own headers in HTTP request ? e.g. This is normal
How can I make a keep alive HTTP request using Python's urllib2?
How can make a link within a facebox window that redirects it to another
I need to send a HTTP request (and get XML response) from Flash that
I need to make an AJAX request from a website to a REST web
I am trying to make a cross domain HTTP request to WCF service (that
I wonder how can I make a cross domain request from a gwt application
Suppose I make an AJAX HTTP Request from jQuery to a back-end PHP script
I want to make a POST HTTP request from a struts2 action to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.