forked from fulldecent/google-voice-numbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
har-to-curl.js
84 lines (66 loc) · 1.73 KB
/
har-to-curl.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* A CommonJS utility for converting a HAR (HTTP Archive) format JSON object to a cURL command string for use on the command line.
*
* @overview
* @author Matthew Caruana Galizia <[email protected]>
* @license MIT
* @copyright Copyright (c) 2012, Matthew Caruana Galizia
* @version 0.3.0
* @preserve
*/
/*jslint node: true */
var harToCurl = function(har) {
'use strict';
if (typeof har === 'string') {
har = JSON.parse(har);
}
if (!har || typeof har !== 'object') {
return;
}
if (har.request) {
return harToCurl.fromEntry(har);
}
if (har.log && Array.isArray(har.log.entries)) {
return harToCurl.fromLog(har.log);
}
if (Array.isArray(har)) {
return harToCurl.fromEntries(har);
}
if (Array.isArray(har.entries)) {
return harToCurl.fromLog(har);
}
};
harToCurl.fromLog = function(log) {
'use strict';
if (!log || !Array.isArray(log.entries)) {
return;
}
return harToCurl.fromEntries(log.entries);
};
harToCurl.fromEntries = function(entries) {
'use strict';
return entries.map(harToCurl.fromEntry);
};
harToCurl.fromEntry = function(entry) {
'use strict';
var command;
if (!entry || !entry.request) {
return '';
}
command = 'curl -X ' + entry.request.method;
if (entry.request.httpVersion === 'HTTP/1.0') {
command += ' -0';
}
if (entry.request.cookies.length) {
command += ' -b "' + entry.request.cookies.map(function(cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
}).join('&') + '"';
}
command += entry.request.headers.map(function(header) {
return ' -H "' + header.name + ': ' + header.value + '"';
}).join('');
if (entry.request.postData) {
command += ' -d "' + entry.request.postData.text + '"';
}
return command + ' ' + entry.request.url;
};