title: Http Request in TypeScript
body: <p>I was trying to convert the following snippet in nodejs to typescript: <a href="https://docs.nodejitsu.com/articles/HTTP/clients/how-to-create-a-HTTP-request/" rel="noreferrer">How do I make Http Request in Nodejs</a></p>
  
  <p>Here is my TypeScript code:</p>
  
  <pre><code>import * as http from 'http';
  
  export class HttpRequest{
  url: string;
  private path: string;
  private host: string;
  private args: Array&lt;Array&lt;string&gt;&gt;;
  
  constructor(url: string, args?: string){
      this.url = url;
      this.processUrl(this.url);
      if(!!args){
          this.processArgs(args);
      }
      this.args = [];
  }
  private processUrl(url: string): void {
      let tld: number = url.lastIndexOf('.')
      let sep: number = url.indexOf('/', tld);
      this.host = url.slice(0, sep);
      this.path = url.slice(sep+1);
  }
  private processArgs(args: string): void {
      let sep: number = args.indexOf('&amp;');
      if(sep &lt; 0){
          return ;
      }
      let argpair: string = args.slice(0, sep);
      let apsep: number = argpair.indexOf('=');
      let k: string = argpair.slice(0, apsep);
      let v: string = argpair.slice(apsep+1);
      this.args.push([k,v]);
      this.processArgs(args.slice(sep+1));
  }
  private preparePath(): string {
      let path: string = `?`;
      this.args.forEach((arg: Array&lt;string&gt;, i: number): void =&gt; {
          let k: string = arg[0];
          let v: string = arg[1];
          path += k + '=' + v;
          if(i == this.args.length-1){
              return ;
          }
          path += '&amp;';
      });
      return path;
  }
  public addArg(key: string, value: string): void {
      try{
          this.args.push([key,value]);
      } catch(err) {
          console.log(err);
      }
  }
  public addArgs(args: Array&lt;Array&lt;string&gt;&gt;): void {
      args.forEach((arg: Array&lt;string&gt;): void =&gt; {
          this.args.push(arg);
      });
  }
  public get(cb: (res: any) =&gt; any): void {
      let opts = {
          'host': this.host,
          'path': `/${this.path}/${this.preparePath()}`
      };
      http.request(opts, (r: http.IncomingMessage): void =&gt; {
          let data = '';
          r.on('data', (chunk: string): void =&gt; {
              console.log('Got chunk: ' + chunk);
              data += chunk;
          });
          r.on('end', (): void =&gt;{
              console.log('Response has ended');
              console.log(data);
              cb(data);
          });
          r.on('error', (err): void =&gt; {
              console.log('Following error occured during request:\n');
              console.log(err);
          })
      }).end();
  }
  public test(): void {
      console.log(this.preparePath());
      console.log(`/${this.path}/${this.preparePath()}`);
  }
  }
  </code></pre>
  
  <p>Here is my test code:</p>
  
  <pre><code>// Test httpRequest
  
  import { HttpRequest } from './httpRequest';
  
  const request = new HttpRequest('www.random.org/integers');
  request.addArg('num', '1');
  request.addArg('min', '1');
  request.addArg('max', '50');
  request.addArg('col', '1');
  request.addArg('base', '10');
  request.addArg('format', 'plain');
  request.addArg('rnd', 'new');
  request.test();
  request.get((res: string): void =&gt; {
      console.log('Response received: ' + res);
  });
  </code></pre>
  
  <p>If this works correctly (I checked the link on Firefox and, it returns a plain text random number) I should get a number as a plain text. However, when I <code>console.log()</code> response, I get nothing. What am I doing wrong here?</p>