@readme/httpsnippet 2.4.5 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -9
- package/package.json +24 -27
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +28 -26
- package/src/helpers/headers.js +18 -8
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +175 -151
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +20 -25
- package/src/targets/clojure/clj_http.js +126 -102
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +93 -90
- package/src/targets/csharp/index.js +3 -5
- package/src/targets/csharp/restsharp.js +20 -22
- package/src/targets/go/index.js +3 -5
- package/src/targets/go/native.js +47 -54
- package/src/targets/http/http1.1.js +27 -37
- package/src/targets/http/index.js +3 -5
- package/src/targets/index.js +2 -4
- package/src/targets/java/asynchttp.js +22 -23
- package/src/targets/java/index.js +3 -5
- package/src/targets/java/nethttp.js +20 -24
- package/src/targets/java/okhttp.js +31 -31
- package/src/targets/java/unirest.js +22 -19
- package/src/targets/javascript/axios.js +47 -42
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +50 -44
- package/src/targets/javascript/xhr.js +48 -44
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +37 -34
- package/src/targets/node/fetch.js +94 -87
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +51 -45
- package/src/targets/node/request.js +69 -69
- package/src/targets/node/unirest.js +70 -68
- package/src/targets/objc/helpers.js +32 -25
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +108 -79
- package/src/targets/ocaml/cohttp.js +32 -30
- package/src/targets/ocaml/index.js +3 -5
- package/src/targets/php/curl.js +86 -82
- package/src/targets/php/guzzle.js +135 -0
- package/src/targets/php/helpers.js +31 -29
- package/src/targets/php/http1.js +38 -49
- package/src/targets/php/http2.js +75 -75
- package/src/targets/php/index.js +5 -5
- package/src/targets/powershell/common.js +35 -30
- package/src/targets/powershell/index.js +3 -5
- package/src/targets/powershell/restmethod.js +3 -5
- package/src/targets/powershell/webrequest.js +3 -5
- package/src/targets/python/helpers.js +42 -37
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -45
- package/src/targets/python/requests.js +48 -72
- package/src/targets/r/httr.js +60 -73
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +41 -35
- package/src/targets/shell/curl.js +108 -43
- package/src/{helpers/shell.js → targets/shell/helpers.js} +7 -9
- package/src/targets/shell/httpie.js +45 -39
- package/src/targets/shell/index.js +4 -5
- package/src/targets/shell/wget.js +20 -22
- package/src/targets/swift/helpers.js +44 -35
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +93 -79
- package/src/.DS_Store +0 -0
package/src/targets/r/httr.js
CHANGED
|
@@ -8,147 +8,134 @@
|
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const { format } = require('util');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
var CodeBuilder = require('../../helpers/code-builder')
|
|
15
|
-
|
|
16
|
-
module.exports = function (source, options) {
|
|
14
|
+
module.exports = function (source) {
|
|
17
15
|
// Start snippet
|
|
18
|
-
|
|
16
|
+
const code = new CodeBuilder();
|
|
19
17
|
|
|
20
18
|
// Import httr
|
|
21
|
-
code.push('library(httr)')
|
|
22
|
-
.blank()
|
|
19
|
+
code.push('library(httr)').blank();
|
|
23
20
|
|
|
24
21
|
// Set URL
|
|
25
|
-
code.push('url <- "%s"', source.url)
|
|
26
|
-
.blank()
|
|
22
|
+
code.push('url <- "%s"', source.url).blank();
|
|
27
23
|
|
|
28
24
|
// Construct query string
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
delete source.queryObj
|
|
25
|
+
const qs = source.queryObj;
|
|
26
|
+
const queryCount = Object.keys(qs).length;
|
|
27
|
+
// eslint-disable-next-line no-param-reassign
|
|
28
|
+
delete source.queryObj.key;
|
|
33
29
|
|
|
34
30
|
if (source.queryString.length === 1) {
|
|
35
|
-
code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString())
|
|
36
|
-
.blank()
|
|
31
|
+
code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString()).blank();
|
|
37
32
|
} else if (source.queryString.length > 1) {
|
|
38
|
-
|
|
33
|
+
let count = 1;
|
|
39
34
|
|
|
40
|
-
code.push('queryString <- list(')
|
|
35
|
+
code.push('queryString <- list(');
|
|
41
36
|
|
|
42
|
-
|
|
37
|
+
Object.keys(qs).forEach(query => {
|
|
38
|
+
// eslint-disable-next-line no-plusplus
|
|
43
39
|
if (count++ !== queryCount - 1) {
|
|
44
|
-
code.push(' %s = "%s",', query, qs[query].toString())
|
|
40
|
+
code.push(' %s = "%s",', query, qs[query].toString());
|
|
45
41
|
} else {
|
|
46
|
-
code.push(' %s = "%s"', query, qs[query].toString())
|
|
42
|
+
code.push(' %s = "%s"', query, qs[query].toString());
|
|
47
43
|
}
|
|
48
|
-
}
|
|
44
|
+
});
|
|
49
45
|
|
|
50
|
-
code.push(')')
|
|
51
|
-
.blank()
|
|
46
|
+
code.push(')').blank();
|
|
52
47
|
}
|
|
53
48
|
|
|
54
49
|
// Construct payload
|
|
55
|
-
|
|
50
|
+
const payload = JSON.stringify(source.postData.text);
|
|
56
51
|
|
|
57
52
|
if (payload) {
|
|
58
|
-
code.push('payload <- %s', payload)
|
|
59
|
-
.blank()
|
|
53
|
+
code.push('payload <- %s', payload).blank();
|
|
60
54
|
}
|
|
61
55
|
|
|
62
56
|
// Define encode
|
|
63
57
|
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
|
|
64
58
|
switch (source.postData.mimeType) {
|
|
65
59
|
case 'application/x-www-form-urlencoded':
|
|
66
|
-
code.push('encode <- "form"')
|
|
67
|
-
|
|
68
|
-
break
|
|
60
|
+
code.push('encode <- "form"').blank();
|
|
61
|
+
break;
|
|
69
62
|
|
|
70
63
|
case 'application/json':
|
|
71
|
-
code.push('encode <- "json"')
|
|
72
|
-
|
|
73
|
-
break
|
|
64
|
+
code.push('encode <- "json"').blank();
|
|
65
|
+
break;
|
|
74
66
|
|
|
75
67
|
case 'multipart/form-data':
|
|
76
|
-
code.push('encode <- "multipart"')
|
|
77
|
-
|
|
78
|
-
break
|
|
68
|
+
code.push('encode <- "multipart"').blank();
|
|
69
|
+
break;
|
|
79
70
|
|
|
80
71
|
default:
|
|
81
|
-
code.push('encode <- "raw"')
|
|
82
|
-
|
|
83
|
-
break
|
|
72
|
+
code.push('encode <- "raw"').blank();
|
|
73
|
+
break;
|
|
84
74
|
}
|
|
85
75
|
}
|
|
86
76
|
|
|
87
77
|
// Construct headers
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
for (head in headers) {
|
|
78
|
+
const headers = source.allHeaders;
|
|
79
|
+
let headerCount = 0;
|
|
80
|
+
let header = '';
|
|
81
|
+
let cookies;
|
|
82
|
+
let accept;
|
|
83
|
+
|
|
84
|
+
Object.keys(headers).forEach(head => {
|
|
96
85
|
if (head.toLowerCase() === 'accept') {
|
|
97
|
-
accept =
|
|
98
|
-
headerCount
|
|
86
|
+
accept = `, accept("${headers[head]}")`;
|
|
87
|
+
headerCount += 1;
|
|
99
88
|
} else if (head.toLowerCase() === 'cookie') {
|
|
100
|
-
cookies =
|
|
101
|
-
headerCount
|
|
89
|
+
cookies = `, set_cookies(\`${headers[head].replace(/;/g, '", `').replace(/` /g, '`').replace(/=/g, '` = "')}")`;
|
|
90
|
+
headerCount += 1;
|
|
102
91
|
} else if (head.toLowerCase() !== 'content-type') {
|
|
103
|
-
header = header + head.replace('-', '_')
|
|
104
|
-
if (headerCount > 1) {
|
|
92
|
+
header = `${header + head.replace('-', '_')} = '${headers[head]}`;
|
|
93
|
+
if (headerCount > 1) {
|
|
94
|
+
header += "', ";
|
|
95
|
+
}
|
|
105
96
|
}
|
|
106
|
-
}
|
|
97
|
+
});
|
|
107
98
|
|
|
108
99
|
// Construct request
|
|
109
|
-
|
|
110
|
-
|
|
100
|
+
const method = source.method;
|
|
101
|
+
let request = format('response <- VERB("%s", url', method);
|
|
111
102
|
|
|
112
103
|
if (payload) {
|
|
113
|
-
request += ', body = payload'
|
|
104
|
+
request += ', body = payload';
|
|
114
105
|
}
|
|
115
106
|
|
|
116
107
|
if (header !== '') {
|
|
117
|
-
request +=
|
|
108
|
+
request += `, add_headers(${header}')`;
|
|
118
109
|
}
|
|
119
110
|
|
|
120
111
|
if (source.queryString.length) {
|
|
121
|
-
request += ', query = queryString'
|
|
112
|
+
request += ', query = queryString';
|
|
122
113
|
}
|
|
123
114
|
|
|
124
|
-
request +=
|
|
115
|
+
request += `, content_type("${source.postData.mimeType}")`;
|
|
125
116
|
|
|
126
117
|
if (typeof accept !== 'undefined') {
|
|
127
|
-
request += accept
|
|
118
|
+
request += accept;
|
|
128
119
|
}
|
|
129
120
|
|
|
130
121
|
if (typeof cookies !== 'undefined') {
|
|
131
|
-
request += cookies
|
|
122
|
+
request += cookies;
|
|
132
123
|
}
|
|
133
124
|
|
|
134
125
|
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
|
|
135
|
-
request += ', encode = encode'
|
|
126
|
+
request += ', encode = encode';
|
|
136
127
|
}
|
|
137
128
|
|
|
138
|
-
request += ')'
|
|
139
|
-
|
|
140
|
-
code.push(request)
|
|
141
|
-
.blank()
|
|
129
|
+
request += ')';
|
|
142
130
|
|
|
143
|
-
|
|
144
|
-
.push('content(response, "text")')
|
|
131
|
+
code.push(request).blank().push('content(response, "text")');
|
|
145
132
|
|
|
146
|
-
return code.join()
|
|
147
|
-
}
|
|
133
|
+
return code.join();
|
|
134
|
+
};
|
|
148
135
|
|
|
149
136
|
module.exports.info = {
|
|
150
137
|
key: 'httr',
|
|
151
138
|
title: 'httr',
|
|
152
139
|
link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
|
|
153
|
-
description: 'httr: Tools for Working with URLs and HTTP'
|
|
154
|
-
}
|
|
140
|
+
description: 'httr: Tools for Working with URLs and HTTP',
|
|
141
|
+
};
|
package/src/targets/r/index.js
CHANGED
|
@@ -1,65 +1,71 @@
|
|
|
1
|
-
|
|
1
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module.exports = function (source) {
|
|
4
|
+
const code = new CodeBuilder();
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
var code = new CodeBuilder()
|
|
7
|
-
|
|
8
|
-
code.push('require \'uri\'')
|
|
9
|
-
.push('require \'net/http\'')
|
|
6
|
+
code.push("require 'uri'").push("require 'net/http'");
|
|
10
7
|
|
|
11
8
|
if (source.uriObj.protocol === 'https:') {
|
|
12
|
-
code.push(
|
|
9
|
+
code.push("require 'openssl'");
|
|
13
10
|
}
|
|
14
11
|
|
|
15
|
-
code.blank()
|
|
12
|
+
code.blank();
|
|
16
13
|
|
|
17
14
|
// To support custom methods we check for the supported methods
|
|
18
15
|
// and if doesn't exist then we build a custom class for it
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
const method = source.method.toUpperCase();
|
|
17
|
+
const methods = [
|
|
18
|
+
'GET',
|
|
19
|
+
'POST',
|
|
20
|
+
'HEAD',
|
|
21
|
+
'DELETE',
|
|
22
|
+
'PATCH',
|
|
23
|
+
'PUT',
|
|
24
|
+
'OPTIONS',
|
|
25
|
+
'COPY',
|
|
26
|
+
'LOCK',
|
|
27
|
+
'UNLOCK',
|
|
28
|
+
'MOVE',
|
|
29
|
+
'TRACE',
|
|
30
|
+
];
|
|
31
|
+
const capMethod = method.charAt(0) + method.substring(1).toLowerCase();
|
|
22
32
|
if (methods.indexOf(method) < 0) {
|
|
23
|
-
code
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
code
|
|
34
|
+
.push('class Net::HTTP::%s < Net::HTTPRequest', capMethod)
|
|
35
|
+
.push(" METHOD = '%s'", method.toUpperCase())
|
|
36
|
+
.push(" REQUEST_HAS_BODY = '%s'", source.postData.text ? 'true' : 'false')
|
|
37
|
+
.push(' RESPONSE_HAS_BODY = true')
|
|
38
|
+
.push('end')
|
|
39
|
+
.blank();
|
|
29
40
|
}
|
|
30
41
|
|
|
31
|
-
code.push('url = URI("%s")', source.fullUrl)
|
|
32
|
-
.blank()
|
|
33
|
-
.push('http = Net::HTTP.new(url.host, url.port)')
|
|
42
|
+
code.push('url = URI("%s")', source.fullUrl).blank().push('http = Net::HTTP.new(url.host, url.port)');
|
|
34
43
|
|
|
35
44
|
if (source.uriObj.protocol === 'https:') {
|
|
36
|
-
code.push('http.use_ssl = true')
|
|
45
|
+
code.push('http.use_ssl = true');
|
|
37
46
|
}
|
|
38
47
|
|
|
39
|
-
code.blank()
|
|
40
|
-
.push('request = Net::HTTP::%s.new(url)', capMethod)
|
|
48
|
+
code.blank().push('request = Net::HTTP::%s.new(url)', capMethod);
|
|
41
49
|
|
|
42
|
-
|
|
50
|
+
const headers = Object.keys(source.allHeaders);
|
|
43
51
|
if (headers.length) {
|
|
44
52
|
headers.forEach(function (key) {
|
|
45
|
-
code.push('request["%s"] = \'%s\'', key, source.allHeaders[key])
|
|
46
|
-
})
|
|
53
|
+
code.push('request["%s"] = \'%s\'', key, source.allHeaders[key]);
|
|
54
|
+
});
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
if (source.postData.text) {
|
|
50
|
-
code.push('request.body = %s', JSON.stringify(source.postData.text))
|
|
58
|
+
code.push('request.body = %s', JSON.stringify(source.postData.text));
|
|
51
59
|
}
|
|
52
60
|
|
|
53
|
-
code.blank()
|
|
54
|
-
.push('response = http.request(request)')
|
|
55
|
-
.push('puts response.read_body')
|
|
61
|
+
code.blank().push('response = http.request(request)').push('puts response.read_body');
|
|
56
62
|
|
|
57
|
-
return code.join()
|
|
58
|
-
}
|
|
63
|
+
return code.join();
|
|
64
|
+
};
|
|
59
65
|
|
|
60
66
|
module.exports.info = {
|
|
61
67
|
key: 'native',
|
|
62
68
|
title: 'net::http',
|
|
63
69
|
link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html',
|
|
64
|
-
description: 'Ruby HTTP client'
|
|
65
|
-
}
|
|
70
|
+
description: 'Ruby HTTP client',
|
|
71
|
+
};
|
|
@@ -8,85 +8,150 @@
|
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const { format } = require('util');
|
|
12
|
+
const helpers = require('./helpers');
|
|
13
|
+
const headerHelpers = require('../../helpers/headers');
|
|
14
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
16
15
|
|
|
17
16
|
module.exports = function (source, options) {
|
|
18
|
-
|
|
17
|
+
const opts = {
|
|
19
18
|
indent: ' ',
|
|
20
19
|
short: false,
|
|
21
|
-
binary: false
|
|
22
|
-
|
|
20
|
+
binary: false,
|
|
21
|
+
globOff: false,
|
|
22
|
+
escapeBrackets: false,
|
|
23
|
+
...options,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const code = new CodeBuilder(opts.indent, opts.indent !== false ? ` \\\n${opts.indent}` : ' ');
|
|
23
27
|
|
|
24
|
-
|
|
28
|
+
const globOption = opts.short ? '-g' : '--globoff';
|
|
29
|
+
const requestOption = opts.short ? '-X' : '--request';
|
|
30
|
+
let formattedUrl = helpers.quote(source.fullUrl);
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
if (opts.escapeBrackets) {
|
|
33
|
+
formattedUrl = formattedUrl.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
code.push('curl %s %s', requestOption, source.method);
|
|
37
|
+
if (opts.globOff) {
|
|
38
|
+
formattedUrl = unescape(formattedUrl);
|
|
39
|
+
code.push(globOption);
|
|
40
|
+
}
|
|
41
|
+
code.push(format('%s%s', opts.short ? '' : '--url ', formattedUrl));
|
|
28
42
|
|
|
29
43
|
if (source.httpVersion === 'HTTP/1.0') {
|
|
30
|
-
code.push(opts.short ? '-0' : '--http1.0')
|
|
44
|
+
code.push(opts.short ? '-0' : '--http1.0');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// if multipart form data, we want to remove the boundary
|
|
48
|
+
if (source.postData.mimeType === 'multipart/form-data') {
|
|
49
|
+
const contentTypeHeaderName = headerHelpers.getHeaderName(source.headersObj, 'content-type');
|
|
50
|
+
const contentTypeHeader = source.headersObj[contentTypeHeaderName];
|
|
51
|
+
|
|
52
|
+
if (contentTypeHeaderName && contentTypeHeader) {
|
|
53
|
+
// remove the leading semi colon and boundary
|
|
54
|
+
// up to the next semi colon or the end of string
|
|
55
|
+
const noBoundary = contentTypeHeader.replace(/; boundary.+?(?=(;|$))/, '');
|
|
56
|
+
|
|
57
|
+
// replace the content-type header with no boundary in both headersObj and allHeaders
|
|
58
|
+
source.headersObj[contentTypeHeaderName] = noBoundary; // eslint-disable-line no-param-reassign
|
|
59
|
+
source.allHeaders[contentTypeHeaderName] = noBoundary; // eslint-disable-line no-param-reassign
|
|
60
|
+
}
|
|
31
61
|
}
|
|
32
62
|
|
|
33
63
|
// construct headers
|
|
34
|
-
Object.keys(source.headersObj)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
64
|
+
Object.keys(source.headersObj)
|
|
65
|
+
.sort()
|
|
66
|
+
.forEach(function (key) {
|
|
67
|
+
const header = format('%s: %s', key, source.headersObj[key]);
|
|
68
|
+
code.push('%s %s', opts.short ? '-H' : '--header', helpers.quote(header));
|
|
69
|
+
});
|
|
38
70
|
|
|
39
71
|
if (source.allHeaders.cookie) {
|
|
40
|
-
code.push('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie))
|
|
72
|
+
code.push('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie));
|
|
41
73
|
}
|
|
42
74
|
|
|
43
75
|
// construct post params
|
|
44
76
|
switch (source.postData.mimeType) {
|
|
45
77
|
case 'multipart/form-data':
|
|
46
|
-
source.postData.params
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
78
|
+
if (source.postData.params) {
|
|
79
|
+
source.postData.params.forEach(function (param) {
|
|
80
|
+
let post = '';
|
|
81
|
+
if (param.fileName) {
|
|
82
|
+
post = format('%s=@%s', param.name, param.fileName);
|
|
83
|
+
} else {
|
|
84
|
+
post = format('%s=%s', param.name, param.value);
|
|
85
|
+
}
|
|
53
86
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
87
|
+
code.push('%s %s', opts.short ? '-F' : '--form', helpers.quote(post));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
57
91
|
|
|
58
92
|
case 'application/x-www-form-urlencoded':
|
|
59
93
|
if (source.postData.params) {
|
|
60
|
-
source.postData.params.
|
|
94
|
+
source.postData.params.forEach(function (param) {
|
|
61
95
|
code.push(
|
|
62
|
-
'%s %s',
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
96
|
+
'%s %s',
|
|
97
|
+
opts.binary ? '--data-binary' : opts.short ? '-d' : '--data',
|
|
98
|
+
helpers.quote(format('%s=%s', param.name, param.value))
|
|
99
|
+
);
|
|
100
|
+
});
|
|
66
101
|
} else {
|
|
67
102
|
code.push(
|
|
68
|
-
'%s %s',
|
|
103
|
+
'%s %s',
|
|
104
|
+
opts.binary ? '--data-binary' : opts.short ? '-d' : '--data',
|
|
69
105
|
helpers.quote(source.postData.text)
|
|
70
|
-
)
|
|
106
|
+
);
|
|
71
107
|
}
|
|
72
|
-
break
|
|
108
|
+
break;
|
|
73
109
|
|
|
74
110
|
default:
|
|
75
111
|
// raw request body
|
|
76
112
|
if (source.postData.text) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
)
|
|
113
|
+
let builtPayload = false;
|
|
114
|
+
|
|
115
|
+
// If we're dealing with a JSON variant, and our payload is JSON let's make it look a little nicer.
|
|
116
|
+
if (headerHelpers.isMimeTypeJson(source.postData.mimeType)) {
|
|
117
|
+
// If our postData is less than 20 characters, let's keep it all on one line so as to not make the snippet
|
|
118
|
+
// overly lengthy
|
|
119
|
+
if (source.postData.text.length > 20) {
|
|
120
|
+
try {
|
|
121
|
+
const jsonPayload = JSON.parse(source.postData.text);
|
|
122
|
+
|
|
123
|
+
// If the JSON object has a single quote we should prepare it inside of a HEREDOC because the single
|
|
124
|
+
// quote in something like `string's` can't be escaped when used with `--data`.
|
|
125
|
+
//
|
|
126
|
+
// Basically this boils down to `--data @- <<EOF...EOF` vs `--data '...'`.
|
|
127
|
+
builtPayload = true;
|
|
128
|
+
code.push(
|
|
129
|
+
source.postData.text.indexOf("'") > 0 ? '%s @- <<EOF\n%s\nEOF' : "%s '\n%s\n'",
|
|
130
|
+
opts.binary ? '--data-binary' : opts.short ? '-d' : '--data',
|
|
131
|
+
JSON.stringify(jsonPayload, null, opts.indent)
|
|
132
|
+
);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
// no-op
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!builtPayload) {
|
|
140
|
+
code.push(
|
|
141
|
+
'%s %s',
|
|
142
|
+
opts.binary ? '--data-binary' : opts.short ? '-d' : '--data',
|
|
143
|
+
helpers.quote(source.postData.text)
|
|
144
|
+
);
|
|
145
|
+
}
|
|
81
146
|
}
|
|
82
147
|
}
|
|
83
148
|
|
|
84
|
-
return code.join()
|
|
85
|
-
}
|
|
149
|
+
return code.join();
|
|
150
|
+
};
|
|
86
151
|
|
|
87
152
|
module.exports.info = {
|
|
88
153
|
key: 'curl',
|
|
89
154
|
title: 'cURL',
|
|
90
155
|
link: 'http://curl.haxx.se/',
|
|
91
|
-
description: 'cURL is a command line tool and library for transferring data with URL syntax'
|
|
92
|
-
}
|
|
156
|
+
description: 'cURL is a command line tool and library for transferring data with URL syntax',
|
|
157
|
+
};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var util = require('util')
|
|
1
|
+
const { format } = require('util');
|
|
4
2
|
|
|
5
3
|
module.exports = {
|
|
6
4
|
/**
|
|
@@ -9,17 +7,17 @@ module.exports = {
|
|
|
9
7
|
* http://wiki.bash-hackers.org/syntax/quoting#strong_quoting
|
|
10
8
|
*/
|
|
11
9
|
quote: function (value) {
|
|
12
|
-
|
|
10
|
+
const safe = /^[a-z0-9-_/.@%^=:]+$/i;
|
|
13
11
|
|
|
14
12
|
// Unless `value` is a simple shell-safe string, quote it.
|
|
15
13
|
if (!safe.test(value)) {
|
|
16
|
-
return
|
|
14
|
+
return format("'%s'", value.replace(/'/g, "'\\''"));
|
|
17
15
|
}
|
|
18
16
|
|
|
19
|
-
return value
|
|
17
|
+
return value;
|
|
20
18
|
},
|
|
21
19
|
|
|
22
20
|
escape: function (value) {
|
|
23
|
-
return value.replace(/\r/g, '\\r').replace(/\n/g, '\\n')
|
|
24
|
-
}
|
|
25
|
-
}
|
|
21
|
+
return value.replace(/\r/g, '\\r').replace(/\n/g, '\\n');
|
|
22
|
+
},
|
|
23
|
+
};
|