Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including http-ext with all npm packages installed. Try it out:

var httpExt = require("http-ext")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

http-ext v0.4.5

the http request client for nodejs module

http-ext

NPM version Build Status Dependency Status Coverage Status

the http request client for nodejs module

Install

You can install http-ext using the Node Package Manager (npm):

$ npm install --save http-ext

Simple Usage

var httpExt = require('http-ext');

httpExt.get('http://www.google.com', function (err, res){
  if (err) return console.log(err);
  console.log(res.body);
});

How to use


httpExt.get(url, [options], callback)

Arguments

  • url: The url to connect to. Can be http or https.
  • options: (all are optional) The following options can be passed:
    • parameters: an object of query parameters
    • headers: an object of headers
    • cookies: an array of cookies
    • allowRedirects: (default: true , only with httpExt.get() ), if true, redirects will be followed
    • maxRedirects: (default: 10 ). For example 1 redirect will allow for one normal request and 1 extra redirected request.
    • timeout: (default: none ). Adds a timeout to the http(s) request. Should be in milliseconds.
    • proxy, if you want to pass your request through a http(s) proxy server:
      • host: eg: "192.168.0.1"
      • port: eg: 8888
      • protocol: (default: 'http' ) can be 'http' or 'https'
    • rejectUnauthorized: validate certificate for request with HTTPS. More here
  • callback(err, res): A callback function which is called when the request is complete. res contains and response ( res ) the body ( res.body )

Example without options

var httpExt = require('http-ext');

httpExt.get('http://www.google.com', function (err, res){
    if (err) return console.log(err);
    console.log(res.body);
});

Example with options

var httpExt = require('http-ext');

httpExt.get('http://posttestserver.com/post.php', {
    parameters: {
        name: 'John',
        lastname: 'Doe'
    },
    headers:{
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
    },
    cookies: [
        'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
        'id=2'
    ]
}, function (err, res){
    if (err){
        console.log(err);
    }else{
        console.log(res.body);
    }
});

httpExt.post(url, [options], callback)

Arguments

  • url: The url to connect to. Can be http or https.
  • options: (all are optional) The following options can be passed:
    • parameters: an object of post parameters (content-type is set to application/x-www-form-urlencoded; charset=UTF-8)
    • json: if you want to send json directly (content-type is set to application/json)
    • body: custom body content you want to send. If used, previous options will be ignored and your custom body will be sent. (content-type will not be set)
    • headers: an object of headers
    • cookies: an array of cookies
    • allowRedirects: (default: false ), if true, redirects will be followed
    • maxRedirects: (default: 10 ). For example 1 redirect will allow for one normal request and 1 extra redirected request.
    • timeout: (default: none). Adds a timeout to the http(s) request. Should be in milliseconds.
    • proxy, if you want to pass your request through a http(s) proxy server:
      • host: eg: "192.168.0.1"
      • port: eg: 8888
      • protocol: (default: 'http' ) can be 'http' or 'https'
    • rejectUnauthorized: validate certificate for request with HTTPS. More here
  • callback(err, res): A callback function which is called when the request is complete. res contains the response ( res ) and the body ( res.body )

Example without extra options

var httpExt = require('httpExt');

httpExt.post('http://posttestserver.com/post.php', {
    parameters: {
        name: 'John',
        lastname: 'Doe'
    }
}, function (err, res){
    if (err){
        console.log(err);
    }else{
        console.log(res.body);
    }
});

Example with options

var httpExt = require('http-ext');

httpExt.post('http://posttestserver.com/post.php', {
    parameters: {
        name: 'John',
        lastname: 'Doe'
    },
    headers:{
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
    },
    cookies: [
        'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
        'id=2'
    ]
}, function (err, res){
    if (err){
        console.log(err);
    }else{
        console.log(res.body);
    }
});

httpExt.put(url, [options], callback)

Same options as httpExt.post(url, [options], callback)


httpExt.delete(url, [options], callback)

Same options as httpExt.post(url, [options], callback)


Sending a custom body

Use the body option to send a custom body (eg. an xml post)

Example

var httpExt = require('http-ext');

httpExt.post('http://posttestserver.com/post.php',{
  body: '<?xml version="1.0" encoding="UTF-8"?>',
  headers: {
    'Content-Type': 'text/xml',
  }},
  function (err, res) {
    if (err){
      console.log(err);
    } else {
      console.log(res.body);
    }
  }
);

Using a http(s) proxy

Example

var httpExt = require('http-ext');

httpExt.post('http://posttestserver.com/post.php', {
  proxy: {
    host: 'localhost',
    port: 8888
  }
}, function (err, res){
  if (err){
    console.log(err);
  }else{
    console.log(res.body);
  }
});

API

(Coming soon)

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using gulp.

License

Copyright (c) 2015 liuxiong. Licensed under the MIT license.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free