Comparing version 0.3.0 to 0.3.1
281
examples.js
var httpreq = require('./httpreq'); | ||
fs = require('fs') | ||
// example1(); // get www.google.com | ||
// example2(); // do some post | ||
// example3(); // same as above + extra headers + cookies | ||
// example4(); // https also works: | ||
// example5(); // uploading some file: | ||
// example6(); // u can use doRequest instead of .get or .post | ||
// example7(); // download a binary file: | ||
example8(); //send json | ||
// example9(); // send your own body content (eg. xml): | ||
// example10(); // set max redirects: | ||
// example11(); // set timeout | ||
// get www.google.com | ||
httpreq.get('http://www.google.com', function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.headers); //headers are stored in res.headers | ||
console.log(res.body); //content of the body is stored in res.body | ||
} | ||
}); | ||
function example1(){ | ||
httpreq.get('http://www.google.com', function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.headers); //headers are stored in res.headers | ||
console.log(res.body); //content of the body is stored in res.body | ||
} | ||
}); | ||
} | ||
// do some post | ||
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); | ||
} | ||
}); | ||
function example2(){ | ||
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); | ||
} | ||
}); | ||
} | ||
// same as above + extra headers + cookies | ||
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); | ||
} | ||
}); | ||
function example3(){ | ||
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); | ||
} | ||
}); | ||
} | ||
// https also works: | ||
httpreq.get('https://graph.facebook.com/19292868552', function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(JSON.parse(res.body)); | ||
} | ||
}); | ||
function example4(){ | ||
httpreq.get('https://graph.facebook.com/19292868552', function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(JSON.parse(res.body)); | ||
} | ||
}); | ||
} | ||
// uploading some file: | ||
httpreq.uploadFiles({ | ||
url: "http://rekognition.com/demo/do_upload/", | ||
parameters:{ | ||
name_space : 'something', | ||
}, | ||
files:{ | ||
fileToUpload: __dirname + "/exampleupload.jpg" | ||
}}, | ||
function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
function example5(){ | ||
httpreq.uploadFiles({ | ||
url: "http://rekognition.com/demo/do_upload/", | ||
parameters:{ | ||
name_space : 'something', | ||
}, | ||
files:{ | ||
fileToUpload: __dirname + "/exampleupload.jpg" | ||
}}, | ||
function (err, res){ | ||
if (err) return console.log(err); | ||
console.log(res.body); | ||
} | ||
}); | ||
}); | ||
} | ||
// u can use doRequest instead of .get or .post | ||
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)); | ||
} | ||
}); | ||
// download a binary file: | ||
httpreq.get('https://ssl.gstatic.com/gb/images/k1_a31af7ac.png', {binary: true}, function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
fs.writeFile(__dirname + '/test.png', res.body, function (err) { | ||
if(err) | ||
console.log("error writing file"); | ||
}); | ||
} | ||
}); | ||
// send your own body content (eg. xml): | ||
httpreq.post('http://posttestserver.com/post.php',{ | ||
body: '<?xml version="1.0" encoding="UTF-8"?>', | ||
headers:{ | ||
'Content-Type': 'text/xml', | ||
}}, | ||
function (err, res) { | ||
function example6(){ | ||
httpreq.doRequest({ | ||
url: 'https://graph.facebook.com/19292868552', | ||
method: 'GET', | ||
parameters: { | ||
name: 'test' | ||
} | ||
}, | ||
function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.body); | ||
console.log(JSON.parse(res.body)); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
// set max redirects: | ||
httpreq.get('http://scobleizer.com/feed/',{ | ||
maxRedirects: 2, // default is 10 | ||
headers:{ | ||
'User-Agent': 'Magnet', //for some reason causes endless redirects on this site... | ||
}}, | ||
function (err, res) { | ||
// download a binary file: | ||
function example7(){ | ||
httpreq.get('https://ssl.gstatic.com/gb/images/k1_a31af7ac.png', {binary: true}, function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.body); | ||
fs.writeFile(__dirname + '/test.png', res.body, function (err) { | ||
if(err) return console.log("error writing file"); | ||
}); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
// send json | ||
function example8(){ | ||
httpreq.post('http://posttestserver.com/post.php',{ | ||
json: {name: 'John', lastname: 'Do'}, | ||
headers:{ | ||
'Content-Type': 'text/xml', | ||
}}, | ||
function (err, res) { | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.body); | ||
} | ||
} | ||
); | ||
} | ||
// send your own body content (eg. xml): | ||
function example9(){ | ||
httpreq.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); | ||
} | ||
} | ||
); | ||
} | ||
// set max redirects: | ||
function example10(){ | ||
httpreq.get('http://scobleizer.com/feed/',{ | ||
maxRedirects: 2, // default is 10 | ||
headers:{ | ||
'User-Agent': 'Magnet', //for some reason causes endless redirects on this site... | ||
}}, | ||
function (err, res) { | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.body); | ||
} | ||
} | ||
); | ||
} | ||
// set timeout | ||
httpreq.get('http://localhost:3000/',{ | ||
timeout: (5 * 1000) // timeout in milliseconds | ||
}, | ||
function (err, res) { | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.body); | ||
function example11(){ | ||
httpreq.get('http://localhost:3000/',{ | ||
timeout: (5 * 1000) // timeout in milliseconds | ||
}, | ||
function (err, res) { | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.body); | ||
} | ||
} | ||
} | ||
); | ||
); | ||
} | ||
@@ -42,6 +42,2 @@ /* | ||
if(moreOptions.maxRedirects === undefined){ | ||
moreOptions.maxRedirects = 10; | ||
} | ||
doRequest(moreOptions, callback); | ||
@@ -62,2 +58,6 @@ } | ||
function doRequest(o, callback){ | ||
if(o.maxRedirects === undefined){ | ||
o.maxRedirects = 10; | ||
} | ||
var hasTimedout = false; | ||
@@ -97,2 +97,6 @@ var chunks = []; | ||
if(o.json){ | ||
body = JSON.stringify(o.json); | ||
} | ||
if(o.body){ | ||
@@ -118,2 +122,6 @@ body = o.body; | ||
if(o.json){ | ||
requestoptions['headers']['Content-Type'] = 'application/json'; | ||
} | ||
if(body){ | ||
@@ -120,0 +128,0 @@ requestoptions['headers']['Content-Length'] = body.length; |
{ | ||
"name": "httpreq", | ||
"description": "node-httpreq is a node.js library to do HTTP(S) requests the easy way", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Sam Decrock", |
102
README.md
@@ -19,6 +19,6 @@ node-httpreq | ||
* [httpreq.uploadFiles(options, callback)](#upload) | ||
* [httpreq.doRequest(options, callback)](#dorequest) | ||
* [Downloading a binary file](#binary) | ||
* [Sending a custom body](#custombody) | ||
* [Using a http(s) proxy](#proxy) | ||
* [httpreq.doRequest(options, callback)](#dorequest) | ||
@@ -31,3 +31,3 @@ --------------------------------------- | ||
- url: The url to connect to. Can be http or https. | ||
- options: (optional) The following options can be passed: | ||
- options: (all are optional) The following options can be passed: | ||
- parameters: an object of query parameters | ||
@@ -37,10 +37,11 @@ - headers: an object of headers | ||
- 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 | ||
- 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 (optional) if you want to pass your request through a http(s) proxy server: | ||
- host: the host of the proxy, eg: "192.168.0.1" | ||
- json: if you want to send json directly (content-type is set to *application/json* ) | ||
- body: custom body content you want to send. Json is ignored when this is used. | ||
- allowRedirects: (default: __true__ , only with httpreq.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: (optional, default: 'http') can be 'http' or 'https' | ||
- protocol: (default: __'http'__ ) can be 'http' or 'https' | ||
- 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__ ) | ||
@@ -54,8 +55,7 @@ | ||
httpreq.get('http://www.google.com', function (err, res){ | ||
if (err){ | ||
console.log(err); | ||
}else{ | ||
console.log(res.headers); | ||
console.log(res.body); | ||
} | ||
if (err) return console.log(err); | ||
console.log(res.statusCode); | ||
console.log(res.headers); | ||
console.log(res.body); | ||
}); | ||
@@ -95,15 +95,16 @@ ``` | ||
- url: The url to connect to. Can be http or https. | ||
- options: (optional) The following options can be passed: | ||
- options: (all are 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 | ||
- maxRedirects: (default: 10). For example 1 redirect will allow for one normal request and 1 extra redirected request. | ||
- binary: true/false (default: __false__ ), if true, res.body will be a buffer containing the binary data | ||
- json: if you want to send json directly (content-type is set to *application/json* ) | ||
- body: custom body content you want to send. Parameters or json is ignored when this is used. | ||
- 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 (optional) if you want to pass your request through a http(s) proxy server: | ||
- host: the host of the proxy, eg: "192.168.0.1" | ||
- proxy, if you want to pass your request through a http(s) proxy server: | ||
- host: eg: "192.168.0.1" | ||
- port: eg: 8888 | ||
- protocol: (optional, default: 'http') can be 'http' or 'https' | ||
- protocol: (default: __'http'__ ) can be 'http' or 'https' | ||
- 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__ ) | ||
@@ -166,3 +167,3 @@ | ||
- cookies: an array of cookies | ||
- binary: true/false (default: false), if true, res.body will a buffer containing the binary data | ||
- binary: true/false (default: __false__ ), if true, res.body will be a buffer containing the binary data | ||
- 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__ ) | ||
@@ -189,48 +190,3 @@ | ||
``` | ||
--------------------------------------- | ||
<a name="dorequest" /> | ||
### 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 | ||
- 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 (optional) if you want to pass your request through a http(s) proxy server: | ||
- host: the host of the proxy, eg: "192.168.0.1" | ||
- port: eg: 8888 | ||
- protocol: (optional, default: 'http') can be 'http' or 'https' | ||
- 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__ | ||
```js | ||
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)); | ||
} | ||
}); | ||
``` | ||
--------------------------------------- | ||
@@ -306,2 +262,10 @@ <a name="binary" /> | ||
--------------------------------------- | ||
<a name="dorequest" /> | ||
### httpreq.doRequest(options, callback) | ||
httpreq.doRequest is internally used by httpreq.get() and httpreq.post(). You can use this directly. Everything is stays the same as httpreq.get() or httpreq.post() except that the following options MUST be passed: | ||
- url: the url to post the files to | ||
- method: 'GET' or 'POST' | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80715
422
264