http-ext
v0.4.5
Published
the http request client for nodejs module
Downloads
11
Readme
http-ext
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)
- httpExt.post(url, [options], callback)
- httpExt.put(url, [options], callback)
- httpExt.delete(url, [options], callback)
- Sending a custom body
- Using a http(s) proxy
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);
}
});
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);
}
});
Same options as httpExt.post(url, [options], callback)
Same options as httpExt.post(url, [options], callback)
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);
}
}
);
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.