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