feat: ✨ Created a mini nodeJS server with NewMan for testing without PostMan GUI.
This will mimic a run in a CD/CI environment or docker container.
This commit is contained in:
19
node_modules/httpreq/LICENSE
generated
vendored
Normal file
19
node_modules/httpreq/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2017 Sam Decrock <sam.decrock@gmail.com>
|
||||
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
383
node_modules/httpreq/README.md
generated
vendored
Normal file
383
node_modules/httpreq/README.md
generated
vendored
Normal file
@@ -0,0 +1,383 @@
|
||||
node-httpreq
|
||||
============
|
||||
|
||||
node-httpreq is a node.js library to do HTTP(S) requests the easy way
|
||||
|
||||
Do GET, POST, PUT, PATCH, DELETE, OPTIONS, upload files, use cookies, change headers, ...
|
||||
|
||||
## Donate
|
||||
|
||||
Feel free [to buy me a pizza 🍕](https://www.buymeacoffee.com/samdecrock)
|
||||
|
||||
## Install
|
||||
|
||||
You can install __httpreq__ using the Node Package Manager (npm):
|
||||
|
||||
npm install httpreq
|
||||
|
||||
## Simple example
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
httpreq.get('http://www.google.com', function (err, res) {
|
||||
if (err) return console.log(err);
|
||||
|
||||
console.log(res.statusCode);
|
||||
console.log(res.headers);
|
||||
console.log(res.body);
|
||||
console.log(res.cookies);
|
||||
});
|
||||
```
|
||||
|
||||
__Using await/async:__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
var res = await httpreq.get('http://www.google.com');
|
||||
|
||||
console.log(res.statusCode);
|
||||
console.log(res.headers);
|
||||
console.log(res.body);
|
||||
console.log(res.cookies);
|
||||
```
|
||||
|
||||
## Use with async/await
|
||||
|
||||
This module has been updated to support async/await.
|
||||
|
||||
In the following examples, simply omit the `callback` parameter and prepend it with `await`.
|
||||
|
||||
__Example:__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
var res = await httpreq.post('http://posttestserver.com/post.php', {
|
||||
parameters: {
|
||||
name: 'John',
|
||||
lastname: 'Doe'
|
||||
}
|
||||
});
|
||||
|
||||
console.log(res.body);
|
||||
```
|
||||
|
||||
## How to use
|
||||
|
||||
* [httpreq.get(url, [options], callback)](#get)
|
||||
* [httpreq.post(url, [options], callback)](#post)
|
||||
* [httpreq.put(url, [options], callback)](#put)
|
||||
* [httpreq.delete(url, [options], callback)](#delete)
|
||||
* [httpreq.options(url, [options], callback)](#options)
|
||||
* [Uploading files](#upload)
|
||||
* [Downloading a binary file](#binary)
|
||||
* [Downloading a file directly to disk](#download)
|
||||
* [Sending a custom body](#custombody)
|
||||
* [Using a http(s) proxy](#proxy)
|
||||
* [httpreq.doRequest(options, callback)](#dorequest)
|
||||
|
||||
---------------------------------------
|
||||
### httpreq.get(url, [options], callback)
|
||||
<a name="get"></a>
|
||||
|
||||
__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
|
||||
- auth: a string for basic authentication. For example `username:password`
|
||||
- binary: true/false (default: false), if true, res.body will a buffer containing the binary data
|
||||
- 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: (default: __'http'__ ) can be 'http' or 'https'
|
||||
- rejectUnauthorized: validate certificate for request with HTTPS. [More here](http://nodejs.org/api/https.html#https_https_request_options_callback)
|
||||
- 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__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
httpreq.get('http://www.google.com', function (err, res){
|
||||
if (err) return console.log(err);
|
||||
|
||||
console.log(res.statusCode);
|
||||
console.log(res.headers);
|
||||
console.log(res.body);
|
||||
});
|
||||
```
|
||||
|
||||
__Example with options__
|
||||
|
||||
```js
|
||||
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)
|
||||
<a name="post"></a>
|
||||
|
||||
__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*)
|
||||
- files: an object of files to upload (content-type is set to *multipart/form-data; boundary=xxx*)
|
||||
- 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
|
||||
- auth: a string for basic authentication. For example `username:password`
|
||||
- binary: true/false (default: __false__ ), if true, res.body will be a buffer containing the binary data
|
||||
- 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.
|
||||
- encodePostParameters: (default: __true__ ), if true, POST/PUT parameters names will be URL encoded.
|
||||
- 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](http://nodejs.org/api/https.html#https_https_request_options_callback)
|
||||
- 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__
|
||||
|
||||
```js
|
||||
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__
|
||||
|
||||
```js
|
||||
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.put(url, [options], callback)
|
||||
<a name="put"></a>
|
||||
|
||||
Same options as [httpreq.post(url, [options], callback)](#post)
|
||||
|
||||
---------------------------------------
|
||||
<a name="delete" />
|
||||
### httpreq.delete(url, [options], callback)
|
||||
|
||||
Same options as [httpreq.post(url, [options], callback)](#post)
|
||||
|
||||
---------------------------------------
|
||||
<a name="options" />
|
||||
### httpreq.options(url, [options], callback)
|
||||
|
||||
Same options as [httpreq.get(url, [options], callback)](#get) except for the ability to follow redirects.
|
||||
|
||||
---------------------------------------
|
||||
<a name="upload" />
|
||||
### Uploading files
|
||||
|
||||
You can still use ```httpreq.uploadFiles({url: 'url', files: {}}, callback)```, but it's easier to just use POST (or PUT):
|
||||
|
||||
__Example__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
httpreq.post('http://posttestserver.com/upload.php', {
|
||||
parameters: {
|
||||
name: 'John',
|
||||
lastname: 'Doe'
|
||||
},
|
||||
files:{
|
||||
myfile: __dirname + "/testupload.jpg",
|
||||
myotherfile: __dirname + "/testupload.jpg"
|
||||
}
|
||||
}, function (err, res){
|
||||
if (err) throw err;
|
||||
});
|
||||
```
|
||||
|
||||
---------------------------------------
|
||||
<a name="binary"></a>
|
||||
### Downloading a binary file
|
||||
To download a binary file, just add __binary: true__ to the options when doing a get or a post.
|
||||
|
||||
__Example__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
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");
|
||||
});
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
---------------------------------------
|
||||
<a name="download"></a>
|
||||
### Downloading a file directly to disk
|
||||
To download a file directly to disk, use the download method provided.
|
||||
|
||||
Downloading is done using a stream, so the data is not stored in memory and directly saved to file.
|
||||
|
||||
__Example__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
httpreq.download(
|
||||
'https://ssl.gstatic.com/gb/images/k1_a31af7ac.png',
|
||||
__dirname + '/test.png'
|
||||
, function (err, progress){
|
||||
if (err) return console.log(err);
|
||||
console.log(progress);
|
||||
}, function (err, res){
|
||||
if (err) return console.log(err);
|
||||
console.log(res);
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
When specifying the `progress` callback (3th parameter), you cannot use async/await.
|
||||
|
||||
---------------------------------------
|
||||
<a name="custombody"></a>
|
||||
### Sending a custom body
|
||||
Use the body option to send a custom body (eg. an xml post)
|
||||
|
||||
__Example__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
---------------------------------------
|
||||
<a name="proxy"></a>
|
||||
### Using a http(s) proxy
|
||||
|
||||
__Example__
|
||||
|
||||
```js
|
||||
var httpreq = require('httpreq');
|
||||
|
||||
httpreq.post('http://posttestserver.com/post.php', {
|
||||
proxy: {
|
||||
host: '10.100.0.126',
|
||||
port: 8888
|
||||
}
|
||||
}, function (err, res){
|
||||
if (err){
|
||||
console.log(err);
|
||||
}else{
|
||||
console.log(res.body);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
---------------------------------------
|
||||
### httpreq.doRequest(options, callback)
|
||||
<a name="dorequest"></a>
|
||||
|
||||
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', 'POST', 'PUT' or 'DELETE'
|
||||
|
||||
## Run tests
|
||||
|
||||
Install all depedencies with
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
Install mocha with
|
||||
|
||||
```bash
|
||||
npm install mocha -g
|
||||
```
|
||||
|
||||
Run tests:
|
||||
```bash
|
||||
mocha test/tests.js
|
||||
```
|
||||
|
||||
Run the async/await tests:
|
||||
```bash
|
||||
mocha test/tests-async.js
|
||||
```
|
||||
|
26
node_modules/httpreq/contributors.md
generated
vendored
Normal file
26
node_modules/httpreq/contributors.md
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
###### Contributors
|
||||
[Sam](https://github.com/SamDecrock)
|
||||
<font color="#999">63 Commits</font> / <font color="#6cc644">2309++</font> / <font color="#bd3c00"> 1040--</font>
|
||||
<font color="#dedede">81.82% <font color="#dedede">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||</font><br><br>
|
||||
[Franklin van de Meent](https://github.com/fvdm)
|
||||
<font color="#999">8 Commits</font> / <font color="#6cc644">51++</font> / <font color="#bd3c00"> 16--</font>
|
||||
<font color="#dedede">10.39% <font color="#dedede">||||||||||||||||||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><br><br>
|
||||
[Russell Beattie](https://github.com/russellbeattie)
|
||||
<font color="#999">1 Commits</font> / <font color="#6cc644">55++</font> / <font color="#bd3c00"> 3--</font>
|
||||
<font color="#dedede">01.30% <font color="#dedede">||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><br><br>
|
||||
[Jason Prickett MSFT](https://github.com/jpricketMSFT)
|
||||
<font color="#999">1 Commits</font> / <font color="#6cc644">5++</font> / <font color="#bd3c00"> 0--</font>
|
||||
<font color="#dedede">01.30% <font color="#dedede">||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><br><br>
|
||||
[null](https://github.com/jjharriso)
|
||||
<font color="#999">1 Commits</font> / <font color="#6cc644">12++</font> / <font color="#bd3c00"> 0--</font>
|
||||
<font color="#dedede">01.30% <font color="#dedede">||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><br><br>
|
||||
[MJJ](https://github.com/mjj2000)
|
||||
<font color="#999">1 Commits</font> / <font color="#6cc644">11++</font> / <font color="#bd3c00"> 1--</font>
|
||||
<font color="#dedede">01.30% <font color="#dedede">||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><br><br>
|
||||
[Jeff Young](https://github.com/jeffyoung)
|
||||
<font color="#999">1 Commits</font> / <font color="#6cc644">19++</font> / <font color="#bd3c00"> 1--</font>
|
||||
<font color="#dedede">01.30% <font color="#dedede">||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><br><br>
|
||||
[Dave Preston](https://github.com/davepreston)
|
||||
<font color="#999">1 Commits</font> / <font color="#6cc644">5++</font> / <font color="#bd3c00"> 0--</font>
|
||||
<font color="#dedede">01.30% <font color="#dedede">||</font><font color="#f4f4f4">||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</font><br><br>
|
||||
###### [Generated](https://github.com/jakeleboeuf/contributor) on Mon May 02 2016 11:08:45 GMT+0200 (CEST)
|
681
node_modules/httpreq/lib/httpreq.js
generated
vendored
Normal file
681
node_modules/httpreq/lib/httpreq.js
generated
vendored
Normal file
@@ -0,0 +1,681 @@
|
||||
/*
|
||||
Copyright (c) 2013 Sam Decrock <sam.decrock@gmail.com>
|
||||
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
var querystring = require ('querystring');
|
||||
var https = require ('https');
|
||||
var http = require ('http');
|
||||
var url = require ('url');
|
||||
var fs = require ('fs');
|
||||
|
||||
|
||||
/**
|
||||
* Generate multipart boundary
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
|
||||
function generateBoundary () {
|
||||
var boundary = '---------------------------';
|
||||
var charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
|
||||
for (var i = 0; i < 29; i++) {
|
||||
boundary += charset.charAt (Math.floor (Math.random () * charset.length));
|
||||
}
|
||||
|
||||
return boundary;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract cookies from headers
|
||||
*
|
||||
* @param headers {object} - Response headers
|
||||
* @returns {array} - Extracted cookie strings
|
||||
*/
|
||||
|
||||
function extractCookies (headers) {
|
||||
var rawcookies = headers['set-cookie'];
|
||||
|
||||
if (!rawcookies) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (rawcookies == []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
var cookies = [];
|
||||
for (var i = 0; i < rawcookies.length; i++) {
|
||||
var rawcookie = rawcookies[i].split (';');
|
||||
if (rawcookie[0]) {
|
||||
cookies.push (rawcookie[0]);
|
||||
}
|
||||
}
|
||||
return cookies;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Custom HTTP request
|
||||
*
|
||||
* @callback callback
|
||||
* @param o {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
function doRequest (o, callback) {
|
||||
// support promises and async/await:
|
||||
if (callback === undefined) {
|
||||
return new Promise((resolve, reject) => {
|
||||
doRequest(o, (err, res) => {
|
||||
err ? reject(err) : resolve(res);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// prevent multiple callbacks
|
||||
var finalCallbackDone = false;
|
||||
function finalCallback (err, res) {
|
||||
if (!finalCallbackDone) {
|
||||
finalCallbackDone = true;
|
||||
callback (err, res);
|
||||
}
|
||||
}
|
||||
|
||||
if (o.maxRedirects === undefined) {
|
||||
o.maxRedirects = 10;
|
||||
}
|
||||
|
||||
if (o.encodePostParameters === undefined) {
|
||||
o.encodePostParameters = true;
|
||||
}
|
||||
|
||||
var chunks = [];
|
||||
var body; // Buffer
|
||||
var contentType;
|
||||
|
||||
var port;
|
||||
var host;
|
||||
var path;
|
||||
var isHttps = false;
|
||||
|
||||
if (o.proxy) {
|
||||
port = o.proxy.port;
|
||||
host = o.proxy.host;
|
||||
path = o.url; // complete url
|
||||
|
||||
if (o.proxy.protocol && o.proxy.protocol.match (/https/)) {
|
||||
isHttps = true;
|
||||
}
|
||||
} else {
|
||||
var reqUrl = url.parse (o.url);
|
||||
host = reqUrl.hostname;
|
||||
path = reqUrl.path;
|
||||
|
||||
if (reqUrl.protocol === 'https:') {
|
||||
isHttps = true;
|
||||
}
|
||||
|
||||
if (reqUrl.port) {
|
||||
port = reqUrl.port;
|
||||
} else if (isHttps) {
|
||||
port = 443;
|
||||
} else {
|
||||
port = 80;
|
||||
}
|
||||
}
|
||||
|
||||
if (o.files && o.files.length > 0 && o.method === 'GET') {
|
||||
var err = new Error ('Can\'t send files using GET');
|
||||
err.code = 'CANT_SEND_FILES_USING_GET';
|
||||
return finalCallback (err);
|
||||
}
|
||||
|
||||
if (o.parameters) {
|
||||
if (o.method === 'GET') {
|
||||
path += '?' + querystring.stringify (o.parameters);
|
||||
} else {
|
||||
body = new Buffer (querystring.stringify (o.parameters), 'utf8');
|
||||
contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
|
||||
}
|
||||
}
|
||||
|
||||
if (o.json) {
|
||||
body = new Buffer (JSON.stringify (o.json), 'utf8');
|
||||
contentType = 'application/json';
|
||||
}
|
||||
|
||||
if (o.files) {
|
||||
var crlf = '\r\n';
|
||||
var boundary = generateBoundary ();
|
||||
var separator = '--' + boundary;
|
||||
var bodyArray = new Array (); // temporary body array
|
||||
|
||||
// if the user wants to POST/PUT files, other parameters need to be encoded using 'Content-Disposition'
|
||||
for (var key in o.parameters) {
|
||||
// According to RFC 2388 (https://www.ietf.org/rfc/rfc2388.txt)
|
||||
// "Field names originally in non-ASCII character sets MAY be encoded
|
||||
// within the value of the "name" parameter using the standard method
|
||||
// described in RFC 2047."
|
||||
// -- encodePostParameters -- true by default and MAY be changed by the user
|
||||
var headerKey = o.encodePostParameters ? encodeURIComponent (key) : key;
|
||||
var encodedParameter = separator + crlf
|
||||
+ 'Content-Disposition: form-data; name="' + headerKey + '"' + crlf
|
||||
+ crlf
|
||||
+ o.parameters[key] + crlf;
|
||||
bodyArray.push (new Buffer (encodedParameter));
|
||||
}
|
||||
|
||||
// now for the files:
|
||||
var haveAlreadyAddedAFile = false;
|
||||
|
||||
for (var file in o.files) {
|
||||
var filepath = o.files[file];
|
||||
var filename = filepath.replace (/\\/g, '/').replace (/.*\//, '');
|
||||
|
||||
var encodedFile = separator + crlf
|
||||
+ 'Content-Disposition: form-data; name="' + file + '"; filename="' + filename + '"' + crlf
|
||||
+ 'Content-Type: application/octet-stream' + crlf
|
||||
+ crlf;
|
||||
|
||||
// add crlf before separator if we have already added a file
|
||||
if (haveAlreadyAddedAFile) {
|
||||
encodedFile = crlf + encodedFile;
|
||||
}
|
||||
|
||||
bodyArray.push (new Buffer (encodedFile));
|
||||
|
||||
// add binary file:
|
||||
bodyArray.push (require ('fs').readFileSync (filepath));
|
||||
|
||||
haveAlreadyAddedAFile = true;
|
||||
}
|
||||
|
||||
var footer = crlf + separator + '--' + crlf;
|
||||
bodyArray.push (new Buffer (footer));
|
||||
|
||||
// set body and contentType:
|
||||
body = Buffer.concat (bodyArray);
|
||||
contentType = 'multipart/form-data; boundary=' + boundary;
|
||||
}
|
||||
|
||||
// overwrites the body if the user passes a body:
|
||||
// clears the content-type
|
||||
if (o.body) {
|
||||
body = new Buffer (o.body, 'utf8');
|
||||
contentType = null;
|
||||
}
|
||||
|
||||
|
||||
var requestoptions = {
|
||||
host: host,
|
||||
port: port,
|
||||
path: path,
|
||||
method: o.method,
|
||||
headers: {}
|
||||
};
|
||||
|
||||
if (!o.redirectCount) {
|
||||
o.redirectCount = 0;
|
||||
}
|
||||
|
||||
if (body) {
|
||||
requestoptions.headers['Content-Length'] = body.length;
|
||||
}
|
||||
|
||||
if (contentType) {
|
||||
requestoptions.headers['Content-Type'] = contentType;
|
||||
}
|
||||
|
||||
if (o.cookies) {
|
||||
requestoptions.headers.Cookie = o.cookies.join ('; ');
|
||||
}
|
||||
|
||||
if (o.rejectUnauthorized !== undefined && isHttps) {
|
||||
requestoptions.rejectUnauthorized = o.rejectUnauthorized;
|
||||
}
|
||||
|
||||
if (isHttps && o.key) {
|
||||
requestoptions.key = o.key;
|
||||
}
|
||||
|
||||
if (isHttps && o.cert) {
|
||||
requestoptions.cert = o.cert;
|
||||
}
|
||||
|
||||
if (isHttps && o.secureProtocol) {
|
||||
requestoptions.secureProtocol = o.secureProtocol;
|
||||
}
|
||||
|
||||
if (isHttps && o.ciphers) {
|
||||
requestoptions.ciphers = o.ciphers;
|
||||
}
|
||||
|
||||
if (isHttps && o.passphrase) {
|
||||
requestoptions.passphrase = o.passphrase;
|
||||
}
|
||||
|
||||
if (isHttps && o.pfx) {
|
||||
requestoptions.pfx = o.pfx;
|
||||
}
|
||||
|
||||
if (isHttps && o.ca) {
|
||||
requestoptions.ca = o.ca;
|
||||
}
|
||||
|
||||
// add custom headers:
|
||||
if (o.headers) {
|
||||
for (var headerkey in o.headers) {
|
||||
requestoptions.headers[headerkey] = o.headers[headerkey];
|
||||
}
|
||||
}
|
||||
|
||||
if (o.agent) {
|
||||
requestoptions.agent = o.agent;
|
||||
}
|
||||
|
||||
if (o.auth) {
|
||||
requestoptions.auth = o.auth;
|
||||
}
|
||||
|
||||
if (o.localAddress) {
|
||||
requestoptions.localAddress = o.localAddress;
|
||||
}
|
||||
|
||||
if (o.secureOptions) {
|
||||
requestoptions.secureOptions = o.secureOptions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process request response
|
||||
*
|
||||
* @param res {object} - Response details
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
function requestResponse (res) {
|
||||
var ended = false;
|
||||
var currentsize = 0;
|
||||
|
||||
var downloadstream = null;
|
||||
if (o.downloadlocation) {
|
||||
downloadstream = fs.createWriteStream (o.downloadlocation);
|
||||
}
|
||||
|
||||
res.on ('data', function (chunk) {
|
||||
if (o.downloadlocation) {
|
||||
downloadstream.write (chunk); //write it to disk, not to memory
|
||||
} else {
|
||||
chunks.push (chunk);
|
||||
}
|
||||
|
||||
if (o.progressCallback) {
|
||||
var totalsize = res.headers['content-length'];
|
||||
if (totalsize) {
|
||||
currentsize += chunk.length;
|
||||
|
||||
o.progressCallback (null, {
|
||||
url: o.url,
|
||||
totalsize: totalsize,
|
||||
currentsize: currentsize,
|
||||
percentage: currentsize * 100 / totalsize
|
||||
});
|
||||
} else {
|
||||
o.progressCallback (new Error ('no content-length specified for file, so no progress monitoring possible'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
res.on ('end', function (err) {
|
||||
ended = true;
|
||||
|
||||
// check for redirects
|
||||
if (res.headers.location && o.allowRedirects) {
|
||||
// Close any open file
|
||||
if (o.downloadlocation) {
|
||||
downloadstream.end ();
|
||||
}
|
||||
|
||||
if (o.redirectCount < o.maxRedirects) {
|
||||
o.redirectCount++;
|
||||
o.url = (new URL(res.headers.location, o.url)).href; // location can be the path only (no base url present)
|
||||
o.cookies = extractCookies (res.headers);
|
||||
return doRequest (o, finalCallback);
|
||||
} else {
|
||||
var err = new Error ('Too many redirects (> ' + o.maxRedirects + ')');
|
||||
err.code = 'TOOMANYREDIRECTS';
|
||||
err.redirects = o.maxRedirects;
|
||||
return finalCallback (err);
|
||||
}
|
||||
}
|
||||
|
||||
if (!o.downloadlocation) {
|
||||
var responsebody = Buffer.concat (chunks);
|
||||
if (!o.binary) {
|
||||
responsebody = responsebody.toString ('utf8');
|
||||
}
|
||||
|
||||
finalCallback (null, {
|
||||
headers: res.headers,
|
||||
statusCode: res.statusCode,
|
||||
body: responsebody,
|
||||
cookies: extractCookies (res.headers)
|
||||
});
|
||||
} else {
|
||||
downloadstream.end (null, null, function () {
|
||||
finalCallback (null, {
|
||||
headers: res.headers,
|
||||
statusCode: res.statusCode,
|
||||
downloadlocation: o.downloadlocation,
|
||||
cookies: extractCookies (res.headers)
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
res.on ('close', function () {
|
||||
if (!ended) {
|
||||
finalCallback (new Error ('Request aborted'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var request;
|
||||
|
||||
// remove headers with undefined keys or values
|
||||
// else we get an error in Node 0.12.0 about "setHeader ()"
|
||||
for (var headerName in requestoptions.headers) {
|
||||
var headerValue = requestoptions.headers[headerName];
|
||||
if (!headerName || !headerValue) {
|
||||
delete requestoptions.headers[headerName];
|
||||
}
|
||||
}
|
||||
|
||||
if (isHttps) {
|
||||
request = https.request (requestoptions, requestResponse);
|
||||
} else {
|
||||
request = http.request (requestoptions, requestResponse);
|
||||
}
|
||||
|
||||
if (o.timeout) {
|
||||
request.setTimeout (parseInt (o.timeout, 10), function () {
|
||||
var err = new Error ('request timed out');
|
||||
err.code = 'TIMEOUT';
|
||||
finalCallback (err);
|
||||
request.abort ();
|
||||
});
|
||||
}
|
||||
|
||||
request.on ('error', function (err) {
|
||||
finalCallback (err);
|
||||
});
|
||||
|
||||
if (body) {
|
||||
request.write (body);
|
||||
}
|
||||
|
||||
request.end ();
|
||||
}
|
||||
|
||||
exports.doRequest = doRequest;
|
||||
|
||||
|
||||
/**
|
||||
* HTTP GET method
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param [options] {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.get = function (url, options, callback) {
|
||||
if (callback === undefined && options && typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var moreOptions = options;
|
||||
moreOptions.url = url;
|
||||
moreOptions.method = 'GET';
|
||||
|
||||
if (moreOptions.allowRedirects === undefined) {
|
||||
moreOptions.allowRedirects = true;
|
||||
}
|
||||
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* HTTP OPTIONS method
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param [options] {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.options = function (url, options, callback) {
|
||||
if (callback === undefined && options && typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var moreOptions = options;
|
||||
moreOptions.url = url;
|
||||
moreOptions.method = 'OPTIONS';
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* HTTP POST method
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param [options] {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.post = function (url, options, callback) {
|
||||
if (callback === undefined && options && typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var moreOptions = options;
|
||||
moreOptions.url = url;
|
||||
moreOptions.method = 'POST';
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* HTTP PUT method
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param [options] {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.put = function (url, options, callback) {
|
||||
if (callback === undefined && options && typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var moreOptions = options;
|
||||
moreOptions.url = url;
|
||||
moreOptions.method = 'PUT';
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* HTTP PATCH method
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param [options] {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.patch = function (url, options, callback) {
|
||||
if (callback === undefined && options && typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var moreOptions = options;
|
||||
moreOptions.url = url;
|
||||
moreOptions.method = 'PATCH';
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* HTTP DELETE method
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param [options] {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.delete = function (url, options, callback) {
|
||||
if (callback === undefined && options && typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var moreOptions = options;
|
||||
moreOptions.url = url;
|
||||
moreOptions.method = 'DELETE';
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* HTTP HEAD method
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param [options] {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.head = function (url, options, callback) {
|
||||
if (callback === undefined && options && typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
var moreOptions = options;
|
||||
moreOptions.url = url;
|
||||
moreOptions.method = 'HEAD';
|
||||
|
||||
if (moreOptions.allowRedirects === undefined) {
|
||||
moreOptions.allowRedirects = true;
|
||||
}
|
||||
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Download a file
|
||||
*
|
||||
* @callback callback
|
||||
* @param url {string} - Request URL
|
||||
* @param downloadlocation {string} - Path where to store file
|
||||
* @param [progressCallback] {function} - Called multiple times during download
|
||||
* @param callback {function} - Called once when download ends
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.download = function (url, downloadlocation, progressCallback, callback) {
|
||||
var options = {};
|
||||
options.url = url;
|
||||
options.method = 'GET';
|
||||
options.downloadlocation = downloadlocation;
|
||||
options.allowRedirects = true;
|
||||
|
||||
// if only 3 args are provided, we assume no progressCallback
|
||||
if (callback === undefined && progressCallback && typeof progressCallback === 'function') {
|
||||
callback = progressCallback;
|
||||
} else {
|
||||
options.progressCallback = progressCallback;
|
||||
}
|
||||
|
||||
return doRequest (options, callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Upload files
|
||||
* old function, can still be used
|
||||
*
|
||||
* @callback callback
|
||||
* @param options {object} - Request options
|
||||
* @param callback [function] - Process response
|
||||
* @returns {void}
|
||||
*/
|
||||
|
||||
exports.uploadFiles = function (options, callback) {
|
||||
var moreOptions = options;
|
||||
moreOptions.method = 'POST';
|
||||
return doRequest (moreOptions, callback);
|
||||
};
|
102
node_modules/httpreq/package.json
generated
vendored
Normal file
102
node_modules/httpreq/package.json
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
{
|
||||
"name": "httpreq",
|
||||
"description": "node-httpreq is a node.js library to do HTTP(S) requests the easy way",
|
||||
"version": "0.5.2",
|
||||
"author": {
|
||||
"name": "Sam Decrock",
|
||||
"url": "https://github.com/SamDecrock/"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/SamDecrock/node-httpreq/issues"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6.15.1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/SamDecrock/node-httpreq.git"
|
||||
},
|
||||
"main": "./lib/httpreq",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"chai": "~1.9.1",
|
||||
"mocha": "~1.20.1",
|
||||
"express": "^4.17.1",
|
||||
"body-parser": "^1.19.0",
|
||||
"multer": "^1.4.2"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Russell Beattie",
|
||||
"email": "russ@russellbeattie.com",
|
||||
"url": "https://github.com/russellbeattie",
|
||||
"contributions": 1,
|
||||
"additions": 55,
|
||||
"deletions": 3,
|
||||
"hireable": null
|
||||
},
|
||||
{
|
||||
"name": "Jason Prickett MSFT",
|
||||
"email": null,
|
||||
"url": "https://github.com/jpricketMSFT",
|
||||
"contributions": 1,
|
||||
"additions": 5,
|
||||
"deletions": 0,
|
||||
"hireable": null
|
||||
},
|
||||
{
|
||||
"name": null,
|
||||
"email": null,
|
||||
"url": "https://github.com/jjharriso",
|
||||
"contributions": 1,
|
||||
"additions": 12,
|
||||
"deletions": 0,
|
||||
"hireable": null
|
||||
},
|
||||
{
|
||||
"name": "Sam",
|
||||
"email": null,
|
||||
"url": "https://github.com/SamDecrock",
|
||||
"contributions": 63,
|
||||
"additions": 2309,
|
||||
"deletions": 1040,
|
||||
"hireable": true
|
||||
},
|
||||
{
|
||||
"name": "MJJ",
|
||||
"email": null,
|
||||
"url": "https://github.com/mjj2000",
|
||||
"contributions": 1,
|
||||
"additions": 11,
|
||||
"deletions": 1,
|
||||
"hireable": true
|
||||
},
|
||||
{
|
||||
"name": "Jeff Young",
|
||||
"email": null,
|
||||
"url": "https://github.com/jeffyoung",
|
||||
"contributions": 1,
|
||||
"additions": 19,
|
||||
"deletions": 1,
|
||||
"hireable": null
|
||||
},
|
||||
{
|
||||
"name": "Dave Preston",
|
||||
"email": null,
|
||||
"url": "https://github.com/davepreston",
|
||||
"contributions": 1,
|
||||
"additions": 5,
|
||||
"deletions": 0,
|
||||
"hireable": null
|
||||
},
|
||||
{
|
||||
"name": "Franklin van de Meent",
|
||||
"email": "fr@nkl.in",
|
||||
"url": "https://github.com/fvdm",
|
||||
"contributions": 8,
|
||||
"additions": 51,
|
||||
"deletions": 16,
|
||||
"hireable": null
|
||||
}
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user