-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (46 loc) · 1.52 KB
/
Copy pathindex.js
File metadata and controls
57 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const _buildCurl = function (params) {
if (!params.url) {
console.error('Input missing: URL');
}
if (!params.verb) {
console.error('Input missing: HTTP verb');
}
let _headers = '';
let _body = '';
try {
if (params.headers) {
if (params.headers['content-length']) {
delete params.headers['content-length'];
}
for (let key in params.headers) {
_headers += `-H '${key}:${params.headers[key]}' `;
}
if (params.body) {
if (params.headers && params.headers['content-type'] && params.headers['content-type'] === 'application/json') {
_body += `-d '${JSON.stringify(params.body)}' `;
} else {
for (let key in params.body) {
_body += `-d '${key}=${params.body[key]}' `;
}
}
}
_headers.length ? _headers = _headers.substring(0, _headers.length - 1) : null;
_body.length ? _body = _body.substring(0, _body.length - 1) : null;
var curl = `curl ${_headers} -X ${params.verb.toUpperCase()} '${params.url}' ${_body}`
return curl;
}
} catch (e) {
console.log(e);
}
};
const printCurl = function (req, res, next) {
var curlParams = {};
curlParams.url = req.protocol + '://' + (req.headers.host || req.hostname) + req.originalUrl;
curlParams.verb = req.method.toUpperCase();
req.headers ? curlParams.headers = req.headers : null;
req.body ? curlParams.body = req.body : null;
console.log(`${_buildCurl(curlParams)}`);
next();
};
module.exports = printCurl;