Getting started with Node.js standard http library

    Table of contents

    This example assumes your QUOTAGUARDSTATIC_URL environment variable is set and contains your unique connection string.

    To access an HTTP API you can use the standard HTTP library in Node.js but must ensure you correctly set the “Host” header to your target hostname, not the proxy hostname.

    var http, options, proxy, url;
    
    http = require("http");
    
    url = require("url");
    
    proxy = url.parse(process.env.QUOTAGUARDSTATIC_URL);
    target  = url.parse("http://ip.jsontest.com/");
    
    options = {
      hostname: proxy.hostname,
      port: proxy.port || 80,
      path: target.href,
      headers: {
        "Proxy-Authorization": "Basic " + (new Buffer(proxy.auth).toString("base64")),
        "Host" : target.hostname
      }
    };
    
    http.get(options, function(res) {
      res.pipe(process.stdout);
      return console.log("status code", res.statusCode);
    });
    

    We have examples for other common scenarios as well, but if you can’t find what you are looking for just send us a Support ticket and we’ll get one sent to you.


    Ready to Get Started?

    Get in touch or create a free trial account