@readme/httpsnippet 2.5.1 → 3.0.3
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 -28
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +27 -25
- package/src/helpers/headers.js +10 -19
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +167 -146
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +18 -23
- package/src/targets/clojure/clj_http.js +103 -87
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +90 -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 +46 -53
- package/src/targets/http/http1.1.js +26 -36
- 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 +43 -38
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +46 -40
- package/src/targets/javascript/xhr.js +42 -38
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +33 -30
- package/src/targets/node/fetch.js +89 -83
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +36 -30
- package/src/targets/node/request.js +64 -64
- package/src/targets/node/unirest.js +56 -55
- package/src/targets/objc/helpers.js +24 -23
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +73 -45
- package/src/targets/ocaml/cohttp.js +29 -27
- 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 +29 -28
- package/src/targets/php/http1.js +33 -44
- package/src/targets/php/http2.js +70 -71
- 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 +37 -34
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -44
- package/src/targets/python/requests.js +47 -72
- package/src/targets/r/httr.js +59 -70
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +39 -33
- package/src/targets/shell/curl.js +74 -52
- package/src/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 +35 -32
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +53 -39
|
@@ -8,153 +8,169 @@
|
|
|
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
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
14
|
-
const helpers = require('../../helpers/headers')
|
|
11
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
+
const helpers = require('../../helpers/headers');
|
|
15
13
|
|
|
16
14
|
const Keyword = function (name) {
|
|
17
|
-
this.name = name
|
|
18
|
-
}
|
|
15
|
+
this.name = name;
|
|
16
|
+
};
|
|
19
17
|
|
|
20
18
|
Keyword.prototype.toString = function () {
|
|
21
|
-
return
|
|
22
|
-
}
|
|
19
|
+
return `:${this.name}`;
|
|
20
|
+
};
|
|
23
21
|
|
|
24
22
|
const File = function (path) {
|
|
25
|
-
this.path = path
|
|
26
|
-
}
|
|
23
|
+
this.path = path;
|
|
24
|
+
};
|
|
27
25
|
|
|
28
26
|
File.prototype.toString = function () {
|
|
29
|
-
return
|
|
30
|
-
}
|
|
27
|
+
return `(clojure.java.io/file "${this.path}")`;
|
|
28
|
+
};
|
|
31
29
|
|
|
32
30
|
const jsType = function (x) {
|
|
33
|
-
return
|
|
34
|
-
|
|
35
|
-
: null
|
|
36
|
-
}
|
|
31
|
+
return typeof x !== 'undefined' ? x.constructor.name.toLowerCase() : null;
|
|
32
|
+
};
|
|
37
33
|
|
|
38
34
|
const objEmpty = function (x) {
|
|
39
|
-
return
|
|
40
|
-
|
|
41
|
-
: false
|
|
42
|
-
}
|
|
35
|
+
return jsType(x) === 'object' ? Object.keys(x).length === 0 : false;
|
|
36
|
+
};
|
|
43
37
|
|
|
44
38
|
const filterEmpty = function (m) {
|
|
45
39
|
Object.keys(m)
|
|
46
|
-
.filter(function (x) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
40
|
+
.filter(function (x) {
|
|
41
|
+
return objEmpty(m[x]);
|
|
42
|
+
})
|
|
43
|
+
.forEach(function (x) {
|
|
44
|
+
// eslint-disable-next-line no-param-reassign
|
|
45
|
+
delete m[x];
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return m;
|
|
49
|
+
};
|
|
50
50
|
|
|
51
51
|
const padBlock = function (x, s) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
// eslint-disable-next-line prefer-spread
|
|
53
|
+
const padding = Array.apply(null, new Array(x))
|
|
54
|
+
.map(function () {
|
|
55
|
+
return ' ';
|
|
55
56
|
})
|
|
56
|
-
.join('')
|
|
57
|
-
|
|
58
|
-
}
|
|
57
|
+
.join('');
|
|
58
|
+
|
|
59
|
+
return s.replace(/\n/g, `\n${padding}`);
|
|
60
|
+
};
|
|
59
61
|
|
|
60
62
|
const jsToEdn = function (js) {
|
|
61
63
|
switch (jsType(js)) {
|
|
62
64
|
case 'string':
|
|
63
|
-
return
|
|
65
|
+
return `"${js.replace(/"/g, '\\"')}"`;
|
|
64
66
|
case 'file':
|
|
65
|
-
return js.toString()
|
|
67
|
+
return js.toString();
|
|
66
68
|
case 'keyword':
|
|
67
|
-
return js.toString()
|
|
69
|
+
return js.toString();
|
|
68
70
|
case 'null':
|
|
69
|
-
return 'nil'
|
|
71
|
+
return 'nil';
|
|
70
72
|
case 'regexp':
|
|
71
|
-
return
|
|
72
|
-
|
|
73
|
+
return `#"${js.source}"`;
|
|
74
|
+
|
|
75
|
+
// simple vertical format
|
|
76
|
+
case 'object': {
|
|
73
77
|
const obj = Object.keys(js)
|
|
74
78
|
.reduce(function (acc, key) {
|
|
75
|
-
const val = padBlock(key.length + 2, jsToEdn(js[key]))
|
|
76
|
-
return acc
|
|
79
|
+
const val = padBlock(key.length + 2, jsToEdn(js[key]));
|
|
80
|
+
return `${acc}:${key} ${val}\n `;
|
|
77
81
|
}, '')
|
|
78
|
-
.trim()
|
|
79
|
-
return
|
|
82
|
+
.trim();
|
|
83
|
+
return `{${padBlock(1, obj)}}`;
|
|
80
84
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
|
|
86
|
+
// simple horizontal format
|
|
87
|
+
case 'array': {
|
|
88
|
+
const arr = js
|
|
89
|
+
.reduce(function (acc, val) {
|
|
90
|
+
return `${acc} ${jsToEdn(val)}`;
|
|
91
|
+
}, '')
|
|
92
|
+
.trim();
|
|
93
|
+
return `[${padBlock(1, arr)}]`;
|
|
86
94
|
}
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
|
|
96
|
+
// 'number' 'boolean'
|
|
97
|
+
default:
|
|
98
|
+
return js.toString();
|
|
89
99
|
}
|
|
90
|
-
}
|
|
100
|
+
};
|
|
91
101
|
|
|
92
102
|
module.exports = function (source, options) {
|
|
93
|
-
const code = new CodeBuilder(options)
|
|
94
|
-
const methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options']
|
|
103
|
+
const code = new CodeBuilder(options);
|
|
104
|
+
const methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
|
|
95
105
|
|
|
96
106
|
if (methods.indexOf(source.method.toLowerCase()) === -1) {
|
|
97
|
-
return code.push('Method not supported').join()
|
|
107
|
+
return code.push('Method not supported').join();
|
|
98
108
|
}
|
|
99
109
|
|
|
100
110
|
const params = {
|
|
101
111
|
headers: source.allHeaders,
|
|
102
|
-
'query-params': source.queryObj
|
|
103
|
-
}
|
|
112
|
+
'query-params': source.queryObj,
|
|
113
|
+
};
|
|
104
114
|
|
|
115
|
+
// eslint-disable-next-line default-case
|
|
105
116
|
switch (source.postData.mimeType) {
|
|
106
117
|
case 'application/json':
|
|
107
|
-
params['content-type'] = new Keyword('json')
|
|
108
|
-
params['form-params'] = source.postData.jsonObj
|
|
109
|
-
delete params.headers[helpers.getHeaderName(params.headers, 'content-type')]
|
|
110
|
-
break
|
|
118
|
+
params['content-type'] = new Keyword('json');
|
|
119
|
+
params['form-params'] = source.postData.jsonObj;
|
|
120
|
+
delete params.headers[helpers.getHeaderName(params.headers, 'content-type')];
|
|
121
|
+
break;
|
|
111
122
|
case 'application/x-www-form-urlencoded':
|
|
112
|
-
params['form-params'] = source.postData.paramsObj
|
|
113
|
-
delete params.headers[helpers.getHeaderName(params.headers, 'content-type')]
|
|
114
|
-
break
|
|
123
|
+
params['form-params'] = source.postData.paramsObj;
|
|
124
|
+
delete params.headers[helpers.getHeaderName(params.headers, 'content-type')];
|
|
125
|
+
break;
|
|
115
126
|
case 'text/plain':
|
|
116
|
-
params.body = source.postData.text
|
|
117
|
-
delete params.headers[helpers.getHeaderName(params.headers, 'content-type')]
|
|
118
|
-
break
|
|
127
|
+
params.body = source.postData.text;
|
|
128
|
+
delete params.headers[helpers.getHeaderName(params.headers, 'content-type')];
|
|
129
|
+
break;
|
|
119
130
|
case 'multipart/form-data':
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
131
|
+
if (source.postData.params) {
|
|
132
|
+
params.multipart = source.postData.params.map(function (x) {
|
|
133
|
+
if (x.fileName && !x.value) {
|
|
134
|
+
return {
|
|
135
|
+
name: x.name,
|
|
136
|
+
content: new File(x.fileName),
|
|
137
|
+
};
|
|
125
138
|
}
|
|
126
|
-
|
|
139
|
+
|
|
127
140
|
return {
|
|
128
141
|
name: x.name,
|
|
129
|
-
content: x.value
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
break
|
|
142
|
+
content: x.value,
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
delete params.headers[helpers.getHeaderName(params.headers, 'content-type')];
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
135
148
|
}
|
|
136
149
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
delete params.headers[helpers.getHeaderName(params.headers, 'accept')]
|
|
141
|
-
break
|
|
150
|
+
if (helpers.getHeader(params.headers, 'accept') === 'application/json') {
|
|
151
|
+
params.accept = new Keyword('json');
|
|
152
|
+
delete params.headers[helpers.getHeaderName(params.headers, 'accept')];
|
|
142
153
|
}
|
|
143
154
|
|
|
144
|
-
code.push(
|
|
155
|
+
code.push("(require '[clj-http.client :as client])\n");
|
|
145
156
|
|
|
146
157
|
if (objEmpty(filterEmpty(params))) {
|
|
147
|
-
code.push('(client/%s "%s")', source.method.toLowerCase(), source.url)
|
|
158
|
+
code.push('(client/%s "%s")', source.method.toLowerCase(), source.url);
|
|
148
159
|
} else {
|
|
149
|
-
code.push(
|
|
160
|
+
code.push(
|
|
161
|
+
'(client/%s "%s" %s)',
|
|
162
|
+
source.method.toLowerCase(),
|
|
163
|
+
source.url,
|
|
164
|
+
padBlock(11 + source.method.length + source.url.length, jsToEdn(filterEmpty(params)))
|
|
165
|
+
);
|
|
150
166
|
}
|
|
151
167
|
|
|
152
|
-
return code.join()
|
|
153
|
-
}
|
|
168
|
+
return code.join();
|
|
169
|
+
};
|
|
154
170
|
|
|
155
171
|
module.exports.info = {
|
|
156
172
|
key: 'clj_http',
|
|
157
173
|
title: 'clj-http',
|
|
158
174
|
link: 'https://github.com/dakrone/clj-http',
|
|
159
|
-
description: 'An idiomatic clojure http client wrapping the apache client.'
|
|
160
|
-
}
|
|
175
|
+
description: 'An idiomatic clojure http client wrapping the apache client.',
|
|
176
|
+
};
|
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
|
|
1
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
2
|
+
const helpers = require('../../helpers/headers');
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
function getDecompressionMethods (source) {
|
|
7
|
-
const acceptEncoding = helpers.getHeader(source.allHeaders, 'accept-encoding')
|
|
4
|
+
function getDecompressionMethods(source) {
|
|
5
|
+
const acceptEncoding = helpers.getHeader(source.allHeaders, 'accept-encoding');
|
|
8
6
|
if (!acceptEncoding) {
|
|
9
|
-
return [] // no decompression
|
|
7
|
+
return []; // no decompression
|
|
10
8
|
}
|
|
11
9
|
|
|
12
10
|
const supportedMethods = {
|
|
13
11
|
gzip: 'DecompressionMethods.GZip',
|
|
14
|
-
deflate: 'DecompressionMethods.Deflate'
|
|
15
|
-
}
|
|
16
|
-
|
|
12
|
+
deflate: 'DecompressionMethods.Deflate',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const methods = [];
|
|
17
16
|
acceptEncoding.split(',').forEach(function (encoding) {
|
|
18
|
-
const match = /\s*([^;\s]+)/.exec(encoding)
|
|
17
|
+
const match = /\s*([^;\s]+)/.exec(encoding);
|
|
19
18
|
if (match) {
|
|
20
|
-
const method = supportedMethods[match[1]]
|
|
19
|
+
const method = supportedMethods[match[1]];
|
|
21
20
|
if (method) {
|
|
22
|
-
methods.push(method)
|
|
21
|
+
methods.push(method);
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
|
-
})
|
|
24
|
+
});
|
|
26
25
|
|
|
27
|
-
return methods
|
|
26
|
+
return methods;
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
module.exports = function (source, options) {
|
|
31
|
-
const opts =
|
|
32
|
-
indent: ' '
|
|
33
|
-
|
|
30
|
+
const opts = {
|
|
31
|
+
indent: ' ',
|
|
32
|
+
...options,
|
|
33
|
+
};
|
|
34
34
|
|
|
35
|
-
const code = new CodeBuilder(opts.indent)
|
|
35
|
+
const code = new CodeBuilder(opts.indent);
|
|
36
36
|
|
|
37
|
-
let clienthandler = ''
|
|
38
|
-
const cookies = !!source.allHeaders.cookie
|
|
39
|
-
const decompressionMethods = getDecompressionMethods(source)
|
|
37
|
+
let clienthandler = '';
|
|
38
|
+
const cookies = !!source.allHeaders.cookie;
|
|
39
|
+
const decompressionMethods = getDecompressionMethods(source);
|
|
40
40
|
if (cookies || decompressionMethods.length) {
|
|
41
|
-
clienthandler = 'clientHandler'
|
|
42
|
-
code.push('var clientHandler = new HttpClientHandler')
|
|
43
|
-
code.push('{')
|
|
41
|
+
clienthandler = 'clientHandler';
|
|
42
|
+
code.push('var clientHandler = new HttpClientHandler');
|
|
43
|
+
code.push('{');
|
|
44
44
|
if (cookies) {
|
|
45
45
|
// enable setting the cookie header
|
|
46
|
-
code.push(1, 'UseCookies = false,')
|
|
46
|
+
code.push(1, 'UseCookies = false,');
|
|
47
47
|
}
|
|
48
48
|
if (decompressionMethods.length) {
|
|
49
49
|
// enable decompression for supported methods
|
|
50
|
-
code.push(1, 'AutomaticDecompression = %s,', decompressionMethods.join(' | '))
|
|
50
|
+
code.push(1, 'AutomaticDecompression = %s,', decompressionMethods.join(' | '));
|
|
51
51
|
}
|
|
52
|
-
code.push('};')
|
|
52
|
+
code.push('};');
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
code.push('var client = new HttpClient(%s);', clienthandler)
|
|
55
|
+
code.push('var client = new HttpClient(%s);', clienthandler);
|
|
56
56
|
|
|
57
|
-
code.push('var request = new HttpRequestMessage')
|
|
58
|
-
code.push('{')
|
|
57
|
+
code.push('var request = new HttpRequestMessage');
|
|
58
|
+
code.push('{');
|
|
59
59
|
|
|
60
|
-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE']
|
|
61
|
-
let method = source.method.toUpperCase()
|
|
62
|
-
if (method &&
|
|
60
|
+
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE'];
|
|
61
|
+
let method = source.method.toUpperCase();
|
|
62
|
+
if (method && methods.indexOf(method) !== -1) {
|
|
63
63
|
// buildin method
|
|
64
|
-
method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}
|
|
64
|
+
method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}`;
|
|
65
65
|
} else {
|
|
66
66
|
// custom method
|
|
67
|
-
method = `new HttpMethod("${method}")
|
|
67
|
+
method = `new HttpMethod("${method}")`;
|
|
68
68
|
}
|
|
69
|
-
code.push(1, 'Method = %s,', method)
|
|
69
|
+
code.push(1, 'Method = %s,', method);
|
|
70
70
|
|
|
71
|
-
code.push(1, 'RequestUri = new Uri("%s"),', source.fullUrl)
|
|
71
|
+
code.push(1, 'RequestUri = new Uri("%s"),', source.fullUrl);
|
|
72
72
|
|
|
73
73
|
const headers = Object.keys(source.allHeaders).filter(function (header) {
|
|
74
74
|
switch (header.toLowerCase()) {
|
|
@@ -76,82 +76,82 @@ module.exports = function (source, options) {
|
|
|
76
76
|
case 'content-length':
|
|
77
77
|
case 'accept-encoding':
|
|
78
78
|
// skip these headers
|
|
79
|
-
return false
|
|
79
|
+
return false;
|
|
80
80
|
default:
|
|
81
|
-
return true
|
|
81
|
+
return true;
|
|
82
82
|
}
|
|
83
|
-
})
|
|
83
|
+
});
|
|
84
84
|
if (headers.length) {
|
|
85
|
-
code.push(1, 'Headers =')
|
|
86
|
-
code.push(1, '{')
|
|
85
|
+
code.push(1, 'Headers =');
|
|
86
|
+
code.push(1, '{');
|
|
87
87
|
headers.forEach(function (key) {
|
|
88
|
-
code.push(2, '{ "%s", "%s" },', key, source.allHeaders[key])
|
|
89
|
-
})
|
|
90
|
-
code.push(1, '},')
|
|
88
|
+
code.push(2, '{ "%s", "%s" },', key, source.allHeaders[key]);
|
|
89
|
+
});
|
|
90
|
+
code.push(1, '},');
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
if (source.postData.text) {
|
|
94
|
-
const contentType = source.postData.mimeType
|
|
94
|
+
const contentType = source.postData.mimeType;
|
|
95
95
|
switch (contentType) {
|
|
96
96
|
case 'application/x-www-form-urlencoded':
|
|
97
|
-
code.push(1, 'Content = new FormUrlEncodedContent(new Dictionary<string, string>')
|
|
98
|
-
code.push(1, '{')
|
|
97
|
+
code.push(1, 'Content = new FormUrlEncodedContent(new Dictionary<string, string>');
|
|
98
|
+
code.push(1, '{');
|
|
99
99
|
source.postData.params.forEach(function (param) {
|
|
100
|
-
code.push(2, '{ "%s", "%s" },', param.name, param.value)
|
|
101
|
-
})
|
|
102
|
-
code.push(1, '}),')
|
|
103
|
-
break
|
|
100
|
+
code.push(2, '{ "%s", "%s" },', param.name, param.value);
|
|
101
|
+
});
|
|
102
|
+
code.push(1, '}),');
|
|
103
|
+
break;
|
|
104
104
|
case 'multipart/form-data':
|
|
105
|
-
code.push(1, 'Content = new MultipartFormDataContent')
|
|
106
|
-
code.push(1, '{')
|
|
105
|
+
code.push(1, 'Content = new MultipartFormDataContent');
|
|
106
|
+
code.push(1, '{');
|
|
107
107
|
source.postData.params.forEach(function (param) {
|
|
108
|
-
code.push(2, 'new StringContent(%s)', JSON.stringify(param.value || ''))
|
|
109
|
-
code.push(2, '{')
|
|
110
|
-
code.push(3, 'Headers =')
|
|
111
|
-
code.push(3, '{')
|
|
108
|
+
code.push(2, 'new StringContent(%s)', JSON.stringify(param.value || ''));
|
|
109
|
+
code.push(2, '{');
|
|
110
|
+
code.push(3, 'Headers =');
|
|
111
|
+
code.push(3, '{');
|
|
112
112
|
if (param.contentType) {
|
|
113
|
-
code.push(4, 'ContentType = new MediaTypeHeaderValue("%s"),', param.contentType)
|
|
113
|
+
code.push(4, 'ContentType = new MediaTypeHeaderValue("%s"),', param.contentType);
|
|
114
114
|
}
|
|
115
|
-
code.push(4, 'ContentDisposition = new ContentDispositionHeaderValue("form-data")')
|
|
116
|
-
code.push(4, '{')
|
|
117
|
-
code.push(5, 'Name = "%s",', param.name)
|
|
115
|
+
code.push(4, 'ContentDisposition = new ContentDispositionHeaderValue("form-data")');
|
|
116
|
+
code.push(4, '{');
|
|
117
|
+
code.push(5, 'Name = "%s",', param.name);
|
|
118
118
|
if (param.fileName) {
|
|
119
|
-
code.push(5, 'FileName = "%s",', param.fileName)
|
|
119
|
+
code.push(5, 'FileName = "%s",', param.fileName);
|
|
120
120
|
}
|
|
121
|
-
code.push(4, '}')
|
|
122
|
-
code.push(3, '}')
|
|
123
|
-
code.push(2, '},')
|
|
124
|
-
})
|
|
121
|
+
code.push(4, '}');
|
|
122
|
+
code.push(3, '}');
|
|
123
|
+
code.push(2, '},');
|
|
124
|
+
});
|
|
125
125
|
|
|
126
|
-
code.push(1, '},')
|
|
127
|
-
break
|
|
126
|
+
code.push(1, '},');
|
|
127
|
+
break;
|
|
128
128
|
default:
|
|
129
|
-
code.push(1, 'Content = new StringContent(%s)', JSON.stringify(source.postData.text || ''))
|
|
130
|
-
code.push(1, '{')
|
|
131
|
-
code.push(2, 'Headers =')
|
|
132
|
-
code.push(2, '{')
|
|
133
|
-
code.push(3, 'ContentType = new MediaTypeHeaderValue("%s")', contentType)
|
|
134
|
-
code.push(2, '}')
|
|
135
|
-
code.push(1, '}')
|
|
136
|
-
break
|
|
129
|
+
code.push(1, 'Content = new StringContent(%s)', JSON.stringify(source.postData.text || ''));
|
|
130
|
+
code.push(1, '{');
|
|
131
|
+
code.push(2, 'Headers =');
|
|
132
|
+
code.push(2, '{');
|
|
133
|
+
code.push(3, 'ContentType = new MediaTypeHeaderValue("%s")', contentType);
|
|
134
|
+
code.push(2, '}');
|
|
135
|
+
code.push(1, '}');
|
|
136
|
+
break;
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
-
code.push('};')
|
|
139
|
+
code.push('};');
|
|
140
140
|
|
|
141
141
|
// send and read response
|
|
142
|
-
code.push('using (var response = await client.SendAsync(request))')
|
|
143
|
-
code.push('{')
|
|
144
|
-
code.push(1, 'response.EnsureSuccessStatusCode();')
|
|
145
|
-
code.push(1, 'var body = await response.Content.ReadAsStringAsync();')
|
|
146
|
-
code.push(1, 'Console.WriteLine(body);')
|
|
147
|
-
code.push('}')
|
|
148
|
-
|
|
149
|
-
return code.join()
|
|
150
|
-
}
|
|
142
|
+
code.push('using (var response = await client.SendAsync(request))');
|
|
143
|
+
code.push('{');
|
|
144
|
+
code.push(1, 'response.EnsureSuccessStatusCode();');
|
|
145
|
+
code.push(1, 'var body = await response.Content.ReadAsStringAsync();');
|
|
146
|
+
code.push(1, 'Console.WriteLine(body);');
|
|
147
|
+
code.push('}');
|
|
148
|
+
|
|
149
|
+
return code.join();
|
|
150
|
+
};
|
|
151
151
|
|
|
152
152
|
module.exports.info = {
|
|
153
153
|
key: 'httpclient',
|
|
154
154
|
title: 'HttpClient',
|
|
155
155
|
link: 'https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient',
|
|
156
|
-
description: '.NET Standard HTTP Client'
|
|
157
|
-
}
|
|
156
|
+
description: '.NET Standard HTTP Client',
|
|
157
|
+
};
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
module.exports = {
|
|
4
2
|
info: {
|
|
5
3
|
key: 'csharp',
|
|
6
4
|
title: 'C#',
|
|
7
5
|
extname: '.cs',
|
|
8
|
-
default: 'restsharp'
|
|
6
|
+
default: 'restsharp',
|
|
9
7
|
},
|
|
10
8
|
|
|
11
9
|
restsharp: require('./restsharp'),
|
|
12
|
-
httpclient: require('./httpclient')
|
|
13
|
-
}
|
|
10
|
+
httpclient: require('./httpclient'),
|
|
11
|
+
};
|
|
@@ -1,34 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
2
|
+
const helpers = require('../../helpers/headers');
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
module.exports = function (source, options) {
|
|
7
|
-
const code = new CodeBuilder()
|
|
8
|
-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']
|
|
4
|
+
module.exports = function (source) {
|
|
5
|
+
const code = new CodeBuilder();
|
|
6
|
+
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
|
|
9
7
|
|
|
10
8
|
if (methods.indexOf(source.method.toUpperCase()) === -1) {
|
|
11
|
-
return 'Method not supported'
|
|
12
|
-
} else {
|
|
13
|
-
code.push('var client = new RestClient("%s");', source.fullUrl)
|
|
14
|
-
code.push('var request = new RestRequest(Method.%s);', source.method.toUpperCase())
|
|
9
|
+
return 'Method not supported';
|
|
15
10
|
}
|
|
16
11
|
|
|
12
|
+
code.push('var client = new RestClient("%s");', source.fullUrl);
|
|
13
|
+
code.push('var request = new RestRequest(Method.%s);', source.method.toUpperCase());
|
|
14
|
+
|
|
17
15
|
// Add headers, including the cookies
|
|
18
|
-
const headers = Object.keys(source.headersObj)
|
|
16
|
+
const headers = Object.keys(source.headersObj);
|
|
19
17
|
|
|
20
18
|
// construct headers
|
|
21
19
|
if (headers.length) {
|
|
22
20
|
headers.forEach(function (key) {
|
|
23
|
-
code.push('request.AddHeader("%s", "%s");', key, source.headersObj[key])
|
|
24
|
-
})
|
|
21
|
+
code.push('request.AddHeader("%s", "%s");', key, source.headersObj[key]);
|
|
22
|
+
});
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
// construct cookies
|
|
28
26
|
if (source.cookies.length) {
|
|
29
27
|
source.cookies.forEach(function (cookie) {
|
|
30
|
-
code.push('request.AddCookie("%s", "%s");', cookie.name, cookie.value)
|
|
31
|
-
})
|
|
28
|
+
code.push('request.AddCookie("%s", "%s");', cookie.name, cookie.value);
|
|
29
|
+
});
|
|
32
30
|
}
|
|
33
31
|
|
|
34
32
|
if (source.postData.text) {
|
|
@@ -36,16 +34,16 @@ module.exports = function (source, options) {
|
|
|
36
34
|
'request.AddParameter("%s", %s, ParameterType.RequestBody);',
|
|
37
35
|
helpers.getHeader(source.allHeaders, 'content-type'),
|
|
38
36
|
JSON.stringify(source.postData.text)
|
|
39
|
-
)
|
|
37
|
+
);
|
|
40
38
|
}
|
|
41
39
|
|
|
42
|
-
code.push('IRestResponse response = client.Execute(request);')
|
|
43
|
-
return code.join()
|
|
44
|
-
}
|
|
40
|
+
code.push('IRestResponse response = client.Execute(request);');
|
|
41
|
+
return code.join();
|
|
42
|
+
};
|
|
45
43
|
|
|
46
44
|
module.exports.info = {
|
|
47
45
|
key: 'restsharp',
|
|
48
46
|
title: 'RestSharp',
|
|
49
47
|
link: 'http://restsharp.org/',
|
|
50
|
-
description: 'Simple REST and HTTP API Client for .NET'
|
|
51
|
-
}
|
|
48
|
+
description: 'Simple REST and HTTP API Client for .NET',
|
|
49
|
+
};
|
package/src/targets/go/index.js
CHANGED