Do GET, POST, upload files, use cookies, change headers, ...
### httpreq.get(url, [options], callback)
Arguments
- url: The url to connect to. Can be http or https.
- options: (optional) The following options can be passed:
- parameters: an object of query parameters
- headers: an object of headers
- cookies: an array of cookies
- binary: true/false (default: false), if true, res.body will a buffer containing the binary data
- body: custom body content you want to send
- allowRedirects: (default: true ...only with httpreq.get ), if true, redirects will be followed
- callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ), the http status code ( res.statusCode ) and the body ( res.body )
Example without options
var httpreq = require('httpreq');
httpreq.get('http://www.google.com', function (err, res){
if (err){
console.log(err);
}else{
console.log(res.headers);
console.log(res.body);
}
});
Example with options
var httpreq = require('httpreq');
httpreq.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);
}
});
### httpreq.post(url, [options], callback)
Arguments
- url: The url to connect to. Can be http or https.
- options: (optional) The following options can be passed:
- parameters: an object of post parameters ( application/x-www-form-urlencoded is used)
- headers: an object of headers
- cookies: an array of cookies
- binary: true/false (default: false), if true, res.body will a buffer containing the binary data
- body: custom body content you want to send. Parameters are ignored when this is used.
- allowRedirects: (default: false), if true, redirects will be followed
- callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ), the http status code ( res.statusCode ) and the body ( res.body )
Example without extra options
var httpreq = require('httpreq');
httpreq.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 httpreq = require('httpreq');
httpreq.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);
}
});
### httpreq.doRequest(options, callback)
This is used by httpreq.get() and httpreq.post()
Arguments
- options: The following options can be passed:
- url: the url to post the files to
- method: 'GET' or 'POST'
- parameters: an object of query/post parameters
- files: an object of files (can be more than one)
- headers: an object of headers
- cookies: an array of cookies
- binary: true/false (default: false), if true, res.body will a buffer containing the binary data
- body: custom body content you want to send
- allowRedirects: (default: false), if true, redirects will be followed
- callback(err, res): A callback function which is called when the request is complete. res contains the headers ( res.headers ), the http status code ( res.statusCode ) and the body ( res.body )
Example
var httpreq = require('httpreq');
httpreq.doRequest({
url: 'https://graph.facebook.com/19292868552',
method: 'GET',
parameters: {
name: 'test'
}
},
function (err, res){
if (err){
console.log(err);
}else{
console.log(JSON.parse(res.body));
}
});