@readme/httpsnippet 3.1.3 → 4.0.2
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 +63 -62
- package/dist/package.json +86 -0
- package/dist/src/fixtures/customTarget.js +15 -0
- package/dist/src/fixtures/mimetypes.js +77 -0
- package/dist/src/fixtures/requests/application-form-encoded.js +69 -0
- package/dist/src/fixtures/requests/application-json.js +72 -0
- package/dist/src/fixtures/requests/cookies.js +51 -0
- package/dist/src/fixtures/requests/custom-method.js +41 -0
- package/dist/src/fixtures/requests/full.js +99 -0
- package/dist/src/fixtures/requests/headers.js +56 -0
- package/dist/src/fixtures/requests/http-insecure.js +45 -0
- package/dist/src/fixtures/requests/jsonObj-multiline.js +59 -0
- package/dist/src/fixtures/requests/jsonObj-null-value.js +61 -0
- package/dist/src/fixtures/requests/multipart-data.js +72 -0
- package/dist/src/fixtures/requests/multipart-file.js +65 -0
- package/dist/src/fixtures/requests/multipart-form-data-no-params.js +56 -0
- package/dist/src/fixtures/requests/multipart-form-data.js +64 -0
- package/dist/src/fixtures/requests/nested.js +64 -0
- package/dist/src/fixtures/requests/query-encoded.js +59 -0
- package/dist/src/fixtures/requests/query.js +64 -0
- package/dist/src/fixtures/requests/short.js +45 -0
- package/dist/src/fixtures/requests/text-plain.js +57 -0
- package/dist/src/fixtures/runCustomFixtures.js +77 -0
- package/dist/src/helpers/code-builder.js +91 -0
- package/dist/src/helpers/form-data.js +157 -0
- package/dist/src/helpers/har-validator.js +76 -0
- package/dist/src/helpers/headers.js +33 -0
- package/dist/src/helpers/reducer.js +19 -0
- package/dist/src/helpers/shell.js +20 -0
- package/dist/src/helpers/utils.js +21 -0
- package/dist/src/index.js +285 -0
- package/dist/src/targets/c/libcurl/client.js +43 -0
- package/dist/src/targets/c/target.js +15 -0
- package/dist/src/targets/clojure/clj_http/client.js +184 -0
- package/dist/src/targets/clojure/target.js +15 -0
- package/dist/src/targets/csharp/httpclient/client.js +162 -0
- package/dist/src/targets/csharp/restsharp/client.js +38 -0
- package/dist/src/targets/csharp/target.js +17 -0
- package/dist/src/targets/go/native/client.js +122 -0
- package/dist/src/targets/go/target.js +15 -0
- package/dist/src/targets/http/http1.1/client.js +79 -0
- package/dist/src/targets/http/target.js +15 -0
- package/dist/src/targets/java/asynchttp/client.js +53 -0
- package/dist/src/targets/java/nethttp/client.js +52 -0
- package/dist/src/targets/java/okhttp/client.js +79 -0
- package/dist/src/targets/java/target.js +21 -0
- package/dist/src/targets/java/unirest/client.js +53 -0
- package/dist/src/targets/javascript/axios/client.js +101 -0
- package/dist/src/targets/javascript/axios/fixtures/application-form-encoded.js +22 -0
- package/dist/src/targets/javascript/axios/fixtures/application-json.js +26 -0
- package/dist/src/targets/javascript/axios/fixtures/cookies.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/custom-method.js +14 -0
- package/dist/src/targets/javascript/axios/fixtures/full.js +26 -0
- package/dist/src/targets/javascript/axios/fixtures/headers.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/http-insecure.js +14 -0
- package/dist/src/targets/javascript/axios/fixtures/jsonObj-multiline.js +19 -0
- package/dist/src/targets/javascript/axios/fixtures/jsonObj-null-value.js +19 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-data.js +22 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-file.js +21 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-form-data-no-params.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/multipart-form-data.js +21 -0
- package/dist/src/targets/javascript/axios/fixtures/nested.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/query-encoded.js +21 -0
- package/dist/src/targets/javascript/axios/fixtures/query.js +18 -0
- package/dist/src/targets/javascript/axios/fixtures/short.js +14 -0
- package/dist/src/targets/javascript/axios/fixtures/text-plain.js +19 -0
- package/dist/src/targets/javascript/fetch/client.js +115 -0
- package/dist/src/targets/javascript/fetch/fixtures/application-form-encoded.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/application-json.js +16 -0
- package/dist/src/targets/javascript/fetch/fixtures/cookies.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/custom-method.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/full.js +13 -0
- package/dist/src/targets/javascript/fetch/fixtures/headers.js +8 -0
- package/dist/src/targets/javascript/fetch/fixtures/http-insecure.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/jsonObj-multiline.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/jsonObj-null-value.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-data.js +9 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-file.js +8 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-form-data-no-params.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/multipart-form-data.js +8 -0
- package/dist/src/targets/javascript/fetch/fixtures/nested.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/query-encoded.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/query.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/short.js +5 -0
- package/dist/src/targets/javascript/fetch/fixtures/text-plain.js +5 -0
- package/dist/src/targets/javascript/jquery/client.js +93 -0
- package/dist/src/targets/javascript/jquery/fixtures/application-form-encoded.js +17 -0
- package/dist/src/targets/javascript/jquery/fixtures/application-json.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/cookies.js +13 -0
- package/dist/src/targets/javascript/jquery/fixtures/custom-method.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/full.js +18 -0
- package/dist/src/targets/javascript/jquery/fixtures/headers.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/http-insecure.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/jsonObj-multiline.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/jsonObj-null-value.js +15 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-data.js +18 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-file.js +17 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-form-data-no-params.js +13 -0
- package/dist/src/targets/javascript/jquery/fixtures/multipart-form-data.js +17 -0
- package/dist/src/targets/javascript/jquery/fixtures/nested.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/query-encoded.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/query.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/short.js +11 -0
- package/dist/src/targets/javascript/jquery/fixtures/text-plain.js +14 -0
- package/dist/src/targets/javascript/target.js +21 -0
- package/dist/src/targets/javascript/xhr/client.js +90 -0
- package/dist/src/targets/javascript/xhr/fixtures/application-form-encoded.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/application-json.js +31 -0
- package/dist/src/targets/javascript/xhr/fixtures/cookies.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/cors.js +10 -0
- package/dist/src/targets/javascript/xhr/fixtures/custom-method.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/full.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/headers.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/http-insecure.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/jsonObj-multiline.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/jsonObj-null-value.js +14 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-data.js +13 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-file.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-form-data-no-params.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/multipart-form-data.js +12 -0
- package/dist/src/targets/javascript/xhr/fixtures/nested.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/query-encoded.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/query.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/short.js +11 -0
- package/dist/src/targets/javascript/xhr/fixtures/text-plain.js +12 -0
- package/dist/src/targets/kotlin/okhttp/client.js +79 -0
- package/dist/src/targets/kotlin/target.js +15 -0
- package/dist/src/targets/node/axios/client.js +87 -0
- package/dist/src/targets/node/axios/fixtures/application-form-encoded.js +19 -0
- package/dist/src/targets/node/axios/fixtures/application-json.js +22 -0
- package/dist/src/targets/node/axios/fixtures/cookies.js +14 -0
- package/dist/src/targets/node/axios/fixtures/custom-method.js +10 -0
- package/dist/src/targets/node/axios/fixtures/full.js +22 -0
- package/dist/src/targets/node/axios/fixtures/headers.js +14 -0
- package/dist/src/targets/node/axios/fixtures/http-insecure.js +10 -0
- package/dist/src/targets/node/axios/fixtures/jsonObj-multiline.js +15 -0
- package/dist/src/targets/node/axios/fixtures/jsonObj-null-value.js +15 -0
- package/dist/src/targets/node/axios/fixtures/multipart-data.js +15 -0
- package/dist/src/targets/node/axios/fixtures/multipart-file.js +15 -0
- package/dist/src/targets/node/axios/fixtures/multipart-form-data-no-params.js +14 -0
- package/dist/src/targets/node/axios/fixtures/multipart-form-data.js +15 -0
- package/dist/src/targets/node/axios/fixtures/nested.js +13 -0
- package/dist/src/targets/node/axios/fixtures/query-encoded.js +13 -0
- package/dist/src/targets/node/axios/fixtures/query.js +13 -0
- package/dist/src/targets/node/axios/fixtures/short.js +10 -0
- package/dist/src/targets/node/axios/fixtures/text-plain.js +15 -0
- package/dist/src/targets/node/fetch/client.js +149 -0
- package/dist/src/targets/node/fetch/fixtures/application-form-encoded.js +15 -0
- package/dist/src/targets/node/fetch/fixtures/application-json.js +18 -0
- package/dist/src/targets/node/fetch/fixtures/cookies.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/custom-method.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/full.js +18 -0
- package/dist/src/targets/node/fetch/fixtures/headers.js +10 -0
- package/dist/src/targets/node/fetch/fixtures/http-insecure.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/jsonObj-multiline.js +11 -0
- package/dist/src/targets/node/fetch/fixtures/jsonObj-null-value.js +11 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-data.js +13 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-file.js +12 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-form-data-no-params.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/multipart-form-data.js +11 -0
- package/dist/src/targets/node/fetch/fixtures/nested.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/query-encoded.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/query.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/short.js +7 -0
- package/dist/src/targets/node/fetch/fixtures/text-plain.js +7 -0
- package/dist/src/targets/node/native/client.js +91 -0
- package/dist/src/targets/node/native/fixtures/application-form-encoded.js +24 -0
- package/dist/src/targets/node/native/fixtures/application-json.js +30 -0
- package/dist/src/targets/node/native/fixtures/cookies.js +22 -0
- package/dist/src/targets/node/native/fixtures/custom-method.js +20 -0
- package/dist/src/targets/node/native/fixtures/full.js +26 -0
- package/dist/src/targets/node/native/fixtures/headers.js +24 -0
- package/dist/src/targets/node/native/fixtures/http-insecure.js +20 -0
- package/dist/src/targets/node/native/fixtures/jsonObj-multiline.js +23 -0
- package/dist/src/targets/node/native/fixtures/jsonObj-null-value.js +23 -0
- package/dist/src/targets/node/native/fixtures/multipart-data.js +23 -0
- package/dist/src/targets/node/native/fixtures/multipart-file.js +23 -0
- package/dist/src/targets/node/native/fixtures/multipart-form-data-no-params.js +22 -0
- package/dist/src/targets/node/native/fixtures/multipart-form-data.js +23 -0
- package/dist/src/targets/node/native/fixtures/nested.js +20 -0
- package/dist/src/targets/node/native/fixtures/query-encoded.js +20 -0
- package/dist/src/targets/node/native/fixtures/query.js +20 -0
- package/dist/src/targets/node/native/fixtures/short.js +20 -0
- package/dist/src/targets/node/native/fixtures/text-plain.js +23 -0
- package/dist/src/targets/node/request/client.js +116 -0
- package/dist/src/targets/node/request/fixtures/application-form-encoded.js +13 -0
- package/dist/src/targets/node/request/fixtures/application-json.js +21 -0
- package/dist/src/targets/node/request/fixtures/cookies.js +11 -0
- package/dist/src/targets/node/request/fixtures/custom-method.js +8 -0
- package/dist/src/targets/node/request/fixtures/full.js +20 -0
- package/dist/src/targets/node/request/fixtures/headers.js +12 -0
- package/dist/src/targets/node/request/fixtures/http-insecure.js +8 -0
- package/dist/src/targets/node/request/fixtures/jsonObj-multiline.js +14 -0
- package/dist/src/targets/node/request/fixtures/jsonObj-null-value.js +14 -0
- package/dist/src/targets/node/request/fixtures/multipart-data.js +20 -0
- package/dist/src/targets/node/request/fixtures/multipart-file.js +19 -0
- package/dist/src/targets/node/request/fixtures/multipart-form-data-no-params.js +12 -0
- package/dist/src/targets/node/request/fixtures/multipart-form-data.js +13 -0
- package/dist/src/targets/node/request/fixtures/nested.js +11 -0
- package/dist/src/targets/node/request/fixtures/query-encoded.js +11 -0
- package/dist/src/targets/node/request/fixtures/query.js +11 -0
- package/dist/src/targets/node/request/fixtures/short.js +8 -0
- package/dist/src/targets/node/request/fixtures/text-plain.js +13 -0
- package/dist/src/targets/node/target.js +24 -0
- package/dist/src/targets/node/unirest/client.js +119 -0
- package/dist/src/targets/node/unirest/fixtures/application-form-encoded.js +15 -0
- package/dist/src/targets/node/unirest/fixtures/application-json.js +32 -0
- package/dist/src/targets/node/unirest/fixtures/cookies.js +12 -0
- package/dist/src/targets/node/unirest/fixtures/custom-method.js +8 -0
- package/dist/src/targets/node/unirest/fixtures/full.js +27 -0
- package/dist/src/targets/node/unirest/fixtures/headers.js +13 -0
- package/dist/src/targets/node/unirest/fixtures/http-insecure.js +8 -0
- package/dist/src/targets/node/unirest/fixtures/jsonObj-multiline.js +15 -0
- package/dist/src/targets/node/unirest/fixtures/jsonObj-null-value.js +15 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-data.js +20 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-file.js +18 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-form-data-no-params.js +11 -0
- package/dist/src/targets/node/unirest/fixtures/multipart-form-data.js +16 -0
- package/dist/src/targets/node/unirest/fixtures/nested.js +13 -0
- package/dist/src/targets/node/unirest/fixtures/query-encoded.js +12 -0
- package/dist/src/targets/node/unirest/fixtures/query.js +16 -0
- package/dist/src/targets/node/unirest/fixtures/short.js +8 -0
- package/dist/src/targets/node/unirest/fixtures/text-plain.js +12 -0
- package/dist/src/targets/objc/helpers.js +59 -0
- package/dist/src/targets/objc/nsurlsession/client.js +151 -0
- package/dist/src/targets/objc/target.js +15 -0
- package/dist/src/targets/ocaml/cohttp/client.js +72 -0
- package/dist/src/targets/ocaml/target.js +15 -0
- package/dist/src/targets/php/curl/client.js +135 -0
- package/dist/src/targets/php/guzzle/client.js +126 -0
- package/dist/src/targets/php/helpers.js +63 -0
- package/dist/src/targets/php/http1/client.js +90 -0
- package/dist/src/targets/php/http2/client.js +128 -0
- package/dist/src/targets/php/target.js +22 -0
- package/dist/src/targets/powershell/common.js +49 -0
- package/dist/src/targets/powershell/restmethod/client.js +13 -0
- package/dist/src/targets/powershell/target.js +17 -0
- package/dist/src/targets/powershell/webrequest/client.js +13 -0
- package/dist/src/targets/python/helpers.js +70 -0
- package/dist/src/targets/python/requests/client.js +161 -0
- package/dist/src/targets/python/target.js +16 -0
- package/dist/src/targets/r/httr/client.js +125 -0
- package/dist/src/targets/r/target.js +15 -0
- package/dist/src/targets/ruby/native/client.js +69 -0
- package/dist/src/targets/ruby/target.js +15 -0
- package/dist/src/targets/shell/curl/client.js +171 -0
- package/dist/src/targets/shell/httpie/client.js +111 -0
- package/dist/src/targets/shell/target.js +20 -0
- package/dist/src/targets/shell/wget/client.js +55 -0
- package/dist/src/targets/swift/helpers.js +80 -0
- package/dist/src/targets/swift/nsurlsession/client.js +156 -0
- package/dist/src/targets/swift/target.js +15 -0
- package/dist/src/targets/targets.js +130 -0
- package/package.json +26 -21
- package/src/helpers/code-builder.js +0 -108
- package/src/helpers/form-data.js +0 -107
- package/src/helpers/headers.js +0 -48
- package/src/helpers/reducer.js +0 -20
- package/src/index.js +0 -361
- package/src/targets/c/index.js +0 -10
- package/src/targets/c/libcurl.js +0 -45
- package/src/targets/clojure/clj_http.js +0 -176
- package/src/targets/clojure/index.js +0 -10
- package/src/targets/csharp/httpclient.js +0 -157
- package/src/targets/csharp/index.js +0 -11
- package/src/targets/csharp/restsharp.js +0 -49
- package/src/targets/go/index.js +0 -10
- package/src/targets/go/native.js +0 -130
- package/src/targets/http/http1.1.js +0 -89
- package/src/targets/http/index.js +0 -10
- package/src/targets/index.js +0 -20
- package/src/targets/java/asynchttp.js +0 -54
- package/src/targets/java/index.js +0 -13
- package/src/targets/java/nethttp.js +0 -59
- package/src/targets/java/okhttp.js +0 -78
- package/src/targets/java/unirest.js +0 -57
- package/src/targets/javascript/axios.js +0 -101
- package/src/targets/javascript/fetch.js +0 -129
- package/src/targets/javascript/index.js +0 -13
- package/src/targets/javascript/jquery.js +0 -89
- package/src/targets/javascript/xhr.js +0 -86
- package/src/targets/kotlin/index.js +0 -10
- package/src/targets/kotlin/okhttp.js +0 -78
- package/src/targets/node/axios.js +0 -76
- package/src/targets/node/fetch.js +0 -145
- package/src/targets/node/index.js +0 -15
- package/src/targets/node/native.js +0 -92
- package/src/targets/node/request.js +0 -126
- package/src/targets/node/unirest.js +0 -120
- package/src/targets/objc/helpers.js +0 -80
- package/src/targets/objc/index.js +0 -10
- package/src/targets/objc/nsurlsession.js +0 -164
- package/src/targets/ocaml/cohttp.js +0 -73
- package/src/targets/ocaml/index.js +0 -10
- package/src/targets/php/curl.js +0 -142
- package/src/targets/php/guzzle.js +0 -135
- package/src/targets/php/helpers.js +0 -87
- package/src/targets/php/http1.js +0 -85
- package/src/targets/php/http2.js +0 -131
- package/src/targets/php/index.js +0 -14
- package/src/targets/powershell/common.js +0 -60
- package/src/targets/powershell/index.js +0 -11
- package/src/targets/powershell/restmethod.js +0 -8
- package/src/targets/powershell/webrequest.js +0 -8
- package/src/targets/python/helpers.js +0 -85
- package/src/targets/python/index.js +0 -11
- package/src/targets/python/requests.js +0 -163
- package/src/targets/r/httr.js +0 -134
- package/src/targets/r/index.js +0 -10
- package/src/targets/ruby/index.js +0 -10
- package/src/targets/ruby/native.js +0 -71
- package/src/targets/shell/curl.js +0 -157
- package/src/targets/shell/helpers.js +0 -23
- package/src/targets/shell/httpie.js +0 -129
- package/src/targets/shell/index.js +0 -13
- package/src/targets/shell/wget.js +0 -52
- package/src/targets/swift/helpers.js +0 -93
- package/src/targets/swift/index.js +0 -10
- package/src/targets/swift/nsurlsession.js +0 -152
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
2
|
-
const helpers = require('../../helpers/headers');
|
|
3
|
-
|
|
4
|
-
function getDecompressionMethods(source) {
|
|
5
|
-
const acceptEncoding = helpers.getHeader(source.allHeaders, 'accept-encoding');
|
|
6
|
-
if (!acceptEncoding) {
|
|
7
|
-
return []; // no decompression
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const supportedMethods = {
|
|
11
|
-
gzip: 'DecompressionMethods.GZip',
|
|
12
|
-
deflate: 'DecompressionMethods.Deflate',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const methods = [];
|
|
16
|
-
acceptEncoding.split(',').forEach(function (encoding) {
|
|
17
|
-
const match = /\s*([^;\s]+)/.exec(encoding);
|
|
18
|
-
if (match) {
|
|
19
|
-
const method = supportedMethods[match[1]];
|
|
20
|
-
if (method) {
|
|
21
|
-
methods.push(method);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
return methods;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = function (source, options) {
|
|
30
|
-
const opts = {
|
|
31
|
-
indent: ' ',
|
|
32
|
-
...options,
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const code = new CodeBuilder(opts.indent);
|
|
36
|
-
|
|
37
|
-
let clienthandler = '';
|
|
38
|
-
const cookies = !!source.allHeaders.cookie;
|
|
39
|
-
const decompressionMethods = getDecompressionMethods(source);
|
|
40
|
-
if (cookies || decompressionMethods.length) {
|
|
41
|
-
clienthandler = 'clientHandler';
|
|
42
|
-
code.push('var clientHandler = new HttpClientHandler');
|
|
43
|
-
code.push('{');
|
|
44
|
-
if (cookies) {
|
|
45
|
-
// enable setting the cookie header
|
|
46
|
-
code.push(1, 'UseCookies = false,');
|
|
47
|
-
}
|
|
48
|
-
if (decompressionMethods.length) {
|
|
49
|
-
// enable decompression for supported methods
|
|
50
|
-
code.push(1, 'AutomaticDecompression = %s,', decompressionMethods.join(' | '));
|
|
51
|
-
}
|
|
52
|
-
code.push('};');
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
code.push('var client = new HttpClient(%s);', clienthandler);
|
|
56
|
-
|
|
57
|
-
code.push('var request = new HttpRequestMessage');
|
|
58
|
-
code.push('{');
|
|
59
|
-
|
|
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
|
-
// buildin method
|
|
64
|
-
method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}`;
|
|
65
|
-
} else {
|
|
66
|
-
// custom method
|
|
67
|
-
method = `new HttpMethod("${method}")`;
|
|
68
|
-
}
|
|
69
|
-
code.push(1, 'Method = %s,', method);
|
|
70
|
-
|
|
71
|
-
code.push(1, 'RequestUri = new Uri("%s"),', source.fullUrl);
|
|
72
|
-
|
|
73
|
-
const headers = Object.keys(source.allHeaders).filter(function (header) {
|
|
74
|
-
switch (header.toLowerCase()) {
|
|
75
|
-
case 'content-type':
|
|
76
|
-
case 'content-length':
|
|
77
|
-
case 'accept-encoding':
|
|
78
|
-
// skip these headers
|
|
79
|
-
return false;
|
|
80
|
-
default:
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
if (headers.length) {
|
|
85
|
-
code.push(1, 'Headers =');
|
|
86
|
-
code.push(1, '{');
|
|
87
|
-
headers.forEach(function (key) {
|
|
88
|
-
code.push(2, '{ "%s", "%s" },', key, source.allHeaders[key]);
|
|
89
|
-
});
|
|
90
|
-
code.push(1, '},');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (source.postData.text) {
|
|
94
|
-
const contentType = source.postData.mimeType;
|
|
95
|
-
switch (contentType) {
|
|
96
|
-
case 'application/x-www-form-urlencoded':
|
|
97
|
-
code.push(1, 'Content = new FormUrlEncodedContent(new Dictionary<string, string>');
|
|
98
|
-
code.push(1, '{');
|
|
99
|
-
source.postData.params.forEach(function (param) {
|
|
100
|
-
code.push(2, '{ "%s", "%s" },', param.name, param.value);
|
|
101
|
-
});
|
|
102
|
-
code.push(1, '}),');
|
|
103
|
-
break;
|
|
104
|
-
case 'multipart/form-data':
|
|
105
|
-
code.push(1, 'Content = new MultipartFormDataContent');
|
|
106
|
-
code.push(1, '{');
|
|
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, '{');
|
|
112
|
-
if (param.contentType) {
|
|
113
|
-
code.push(4, 'ContentType = new MediaTypeHeaderValue("%s"),', param.contentType);
|
|
114
|
-
}
|
|
115
|
-
code.push(4, 'ContentDisposition = new ContentDispositionHeaderValue("form-data")');
|
|
116
|
-
code.push(4, '{');
|
|
117
|
-
code.push(5, 'Name = "%s",', param.name);
|
|
118
|
-
if (param.fileName) {
|
|
119
|
-
code.push(5, 'FileName = "%s",', param.fileName);
|
|
120
|
-
}
|
|
121
|
-
code.push(4, '}');
|
|
122
|
-
code.push(3, '}');
|
|
123
|
-
code.push(2, '},');
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
code.push(1, '},');
|
|
127
|
-
break;
|
|
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;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
code.push('};');
|
|
140
|
-
|
|
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
|
-
};
|
|
151
|
-
|
|
152
|
-
module.exports.info = {
|
|
153
|
-
key: 'httpclient',
|
|
154
|
-
title: 'HttpClient',
|
|
155
|
-
link: 'https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient',
|
|
156
|
-
description: '.NET Standard HTTP Client',
|
|
157
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
2
|
-
const helpers = require('../../helpers/headers');
|
|
3
|
-
|
|
4
|
-
module.exports = function (source) {
|
|
5
|
-
const code = new CodeBuilder();
|
|
6
|
-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
|
|
7
|
-
|
|
8
|
-
if (methods.indexOf(source.method.toUpperCase()) === -1) {
|
|
9
|
-
return 'Method not supported';
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
code.push('var client = new RestClient("%s");', source.fullUrl);
|
|
13
|
-
code.push('var request = new RestRequest(Method.%s);', source.method.toUpperCase());
|
|
14
|
-
|
|
15
|
-
// Add headers, including the cookies
|
|
16
|
-
const headers = Object.keys(source.headersObj);
|
|
17
|
-
|
|
18
|
-
// construct headers
|
|
19
|
-
if (headers.length) {
|
|
20
|
-
headers.forEach(function (key) {
|
|
21
|
-
code.push('request.AddHeader("%s", "%s");', key, source.headersObj[key]);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// construct cookies
|
|
26
|
-
if (source.cookies.length) {
|
|
27
|
-
source.cookies.forEach(function (cookie) {
|
|
28
|
-
code.push('request.AddCookie("%s", "%s");', cookie.name, cookie.value);
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (source.postData.text) {
|
|
33
|
-
code.push(
|
|
34
|
-
'request.AddParameter("%s", %s, ParameterType.RequestBody);',
|
|
35
|
-
helpers.getHeader(source.allHeaders, 'content-type'),
|
|
36
|
-
JSON.stringify(source.postData.text)
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
code.push('IRestResponse response = client.Execute(request);');
|
|
41
|
-
return code.join();
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
module.exports.info = {
|
|
45
|
-
key: 'restsharp',
|
|
46
|
-
title: 'RestSharp',
|
|
47
|
-
link: 'http://restsharp.org/',
|
|
48
|
-
description: 'Simple REST and HTTP API Client for .NET',
|
|
49
|
-
};
|
package/src/targets/go/index.js
DELETED
package/src/targets/go/native.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for native Go.
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @montanaflynn
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
|
|
13
|
-
module.exports = function (source, options) {
|
|
14
|
-
// Let's Go!
|
|
15
|
-
const code = new CodeBuilder('\t');
|
|
16
|
-
|
|
17
|
-
// Define Options
|
|
18
|
-
const opts = {
|
|
19
|
-
showBoilerplate: true,
|
|
20
|
-
checkErrors: false,
|
|
21
|
-
printBody: true,
|
|
22
|
-
timeout: -1,
|
|
23
|
-
...options,
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const errorPlaceholder = opts.checkErrors ? 'err' : '_';
|
|
27
|
-
|
|
28
|
-
const indent = opts.showBoilerplate ? 1 : 0;
|
|
29
|
-
|
|
30
|
-
const errorCheck = function () {
|
|
31
|
-
if (opts.checkErrors) {
|
|
32
|
-
code
|
|
33
|
-
.push(indent, 'if err != nil {')
|
|
34
|
-
.push(indent + 1, 'panic(err)')
|
|
35
|
-
.push(indent, '}');
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// Create boilerplate
|
|
40
|
-
if (opts.showBoilerplate) {
|
|
41
|
-
code.push('package main').blank().push('import (').push(indent, '"fmt"');
|
|
42
|
-
|
|
43
|
-
if (opts.timeout > 0) {
|
|
44
|
-
code.push(indent, '"time"');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (source.postData.text) {
|
|
48
|
-
code.push(indent, '"strings"');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
code.push(indent, '"net/http"');
|
|
52
|
-
|
|
53
|
-
if (opts.printBody) {
|
|
54
|
-
code.push(indent, '"io/ioutil"');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
code.push(')').blank().push('func main() {').blank();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Create client
|
|
61
|
-
let client;
|
|
62
|
-
if (opts.timeout > 0) {
|
|
63
|
-
client = 'client';
|
|
64
|
-
code
|
|
65
|
-
.push(indent, 'client := http.Client{')
|
|
66
|
-
.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
|
|
67
|
-
.push(indent, '}')
|
|
68
|
-
.blank();
|
|
69
|
-
} else {
|
|
70
|
-
client = 'http.DefaultClient';
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
code.push(indent, 'url := "%s"', source.fullUrl).blank();
|
|
74
|
-
|
|
75
|
-
// If we have body content or not create the var and reader or nil
|
|
76
|
-
if (source.postData.text) {
|
|
77
|
-
code
|
|
78
|
-
.push(indent, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
|
|
79
|
-
.blank()
|
|
80
|
-
.push(indent, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
|
|
81
|
-
.blank();
|
|
82
|
-
} else {
|
|
83
|
-
code.push(indent, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method).blank();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
errorCheck();
|
|
87
|
-
|
|
88
|
-
// Add headers
|
|
89
|
-
if (Object.keys(source.allHeaders).length) {
|
|
90
|
-
Object.keys(source.allHeaders).forEach(function (key) {
|
|
91
|
-
code.push(indent, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key]);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
code.blank();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Make request
|
|
98
|
-
code.push(indent, 'res, %s := %s.Do(req)', errorPlaceholder, client);
|
|
99
|
-
errorCheck();
|
|
100
|
-
|
|
101
|
-
// Get Body
|
|
102
|
-
if (opts.printBody) {
|
|
103
|
-
code
|
|
104
|
-
.blank()
|
|
105
|
-
.push(indent, 'defer res.Body.Close()')
|
|
106
|
-
.push(indent, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder);
|
|
107
|
-
errorCheck();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Print it
|
|
111
|
-
code.blank().push(indent, 'fmt.Println(res)');
|
|
112
|
-
|
|
113
|
-
if (opts.printBody) {
|
|
114
|
-
code.push(indent, 'fmt.Println(string(body))');
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// End main block
|
|
118
|
-
if (opts.showBoilerplate) {
|
|
119
|
-
code.blank().push('}');
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return code.join();
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
module.exports.info = {
|
|
126
|
-
key: 'native',
|
|
127
|
-
title: 'NewRequest',
|
|
128
|
-
link: 'http://golang.org/pkg/net/http/#NewRequest',
|
|
129
|
-
description: 'Golang HTTP client request',
|
|
130
|
-
};
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator to generate raw HTTP/1.1 request strings,
|
|
4
|
-
* in accordance to the RFC 7230 (and RFC 7231) specifications.
|
|
5
|
-
*
|
|
6
|
-
* @author
|
|
7
|
-
* @irvinlim
|
|
8
|
-
*
|
|
9
|
-
* For any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const CRLF = '\r\n';
|
|
13
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
14
|
-
const { format } = require('util');
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Request follows the request message format in accordance to RFC 7230, Section 3.
|
|
18
|
-
* Each section is prepended with the RFC and section number.
|
|
19
|
-
* See more at https://tools.ietf.org/html/rfc7230#section-3.
|
|
20
|
-
*/
|
|
21
|
-
module.exports = function (source, options) {
|
|
22
|
-
const opts = {
|
|
23
|
-
absoluteURI: false,
|
|
24
|
-
autoContentLength: true,
|
|
25
|
-
autoHost: true,
|
|
26
|
-
...options,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// RFC 7230 Section 3. Message Format
|
|
30
|
-
// All lines have no indentation, and should be terminated with CRLF.
|
|
31
|
-
const code = new CodeBuilder('', CRLF);
|
|
32
|
-
|
|
33
|
-
// RFC 7230 Section 5.3. Request Target
|
|
34
|
-
// Determines if the Request-Line should use 'absolute-form' or 'origin-form'.
|
|
35
|
-
// Basically it means whether the "http://domain.com" will prepend the full url.
|
|
36
|
-
const requestUrl = opts.absoluteURI ? source.fullUrl : source.uriObj.path;
|
|
37
|
-
|
|
38
|
-
// RFC 7230 Section 3.1.1. Request-Line
|
|
39
|
-
code.push('%s %s %s', source.method, requestUrl, source.httpVersion);
|
|
40
|
-
|
|
41
|
-
// RFC 7231 Section 5. Header Fields
|
|
42
|
-
Object.keys(source.allHeaders).forEach(function (key) {
|
|
43
|
-
// Capitalize header keys, even though it's not required by the spec.
|
|
44
|
-
const keyCapitalized = key.toLowerCase().replace(/(^|-)(\w)/g, function (x) {
|
|
45
|
-
return x.toUpperCase();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
code.push('%s', format('%s: %s', keyCapitalized, source.allHeaders[key]));
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// RFC 7230 Section 5.4. Host
|
|
52
|
-
// Automatically set Host header if option is on and on header already exists.
|
|
53
|
-
if (opts.autoHost && Object.keys(source.allHeaders).indexOf('host') === -1) {
|
|
54
|
-
code.push('Host: %s', source.uriObj.host);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// RFC 7230 Section 3.3.3. Message Body Length
|
|
58
|
-
// Automatically set Content-Length header if option is on, postData is present and no header already exists.
|
|
59
|
-
if (
|
|
60
|
-
opts.autoContentLength &&
|
|
61
|
-
source.postData.text &&
|
|
62
|
-
Object.keys(source.allHeaders).indexOf('content-length') === -1
|
|
63
|
-
) {
|
|
64
|
-
code.push('Content-Length: %d', Buffer.byteLength(source.postData.text, 'ascii'));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Add extra line after header section.
|
|
68
|
-
code.blank();
|
|
69
|
-
|
|
70
|
-
// Separate header section and message body section.
|
|
71
|
-
const headerSection = code.join();
|
|
72
|
-
let messageBody = '';
|
|
73
|
-
|
|
74
|
-
// RFC 7230 Section 3.3. Message Body
|
|
75
|
-
if (source.postData.text) {
|
|
76
|
-
messageBody = source.postData.text;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// RFC 7230 Section 3. Message Format
|
|
80
|
-
// Extra CRLF separating the headers from the body.
|
|
81
|
-
return headerSection + CRLF + messageBody;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
module.exports.info = {
|
|
85
|
-
key: '1.1',
|
|
86
|
-
title: 'HTTP/1.1',
|
|
87
|
-
link: 'https://tools.ietf.org/html/rfc7230',
|
|
88
|
-
description: 'HTTP/1.1 request string in accordance with RFC 7230',
|
|
89
|
-
};
|
package/src/targets/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
c: require('./c'),
|
|
3
|
-
clojure: require('./clojure'),
|
|
4
|
-
csharp: require('./csharp'),
|
|
5
|
-
go: require('./go'),
|
|
6
|
-
http: require('./http'),
|
|
7
|
-
java: require('./java'),
|
|
8
|
-
javascript: require('./javascript'),
|
|
9
|
-
kotlin: require('./kotlin'),
|
|
10
|
-
node: require('./node'),
|
|
11
|
-
objc: require('./objc'),
|
|
12
|
-
ocaml: require('./ocaml'),
|
|
13
|
-
php: require('./php'),
|
|
14
|
-
powershell: require('./powershell'),
|
|
15
|
-
python: require('./python'),
|
|
16
|
-
r: require('./r'),
|
|
17
|
-
ruby: require('./ruby'),
|
|
18
|
-
shell: require('./shell'),
|
|
19
|
-
swift: require('./swift'),
|
|
20
|
-
};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* Asynchronous Http and WebSocket Client library for Java
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @windard
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
|
|
13
|
-
module.exports = function (source, options) {
|
|
14
|
-
const opts = {
|
|
15
|
-
indent: ' ',
|
|
16
|
-
...options,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const code = new CodeBuilder(opts.indent);
|
|
20
|
-
|
|
21
|
-
code.push('AsyncHttpClient client = new DefaultAsyncHttpClient();');
|
|
22
|
-
|
|
23
|
-
code.push(`client.prepare("${source.method.toUpperCase()}", "${source.fullUrl}")`);
|
|
24
|
-
|
|
25
|
-
// Add headers, including the cookies
|
|
26
|
-
const headers = Object.keys(source.allHeaders);
|
|
27
|
-
|
|
28
|
-
// construct headers
|
|
29
|
-
if (headers.length) {
|
|
30
|
-
headers.forEach(function (key) {
|
|
31
|
-
code.push(1, '.setHeader("%s", "%s")', key, source.allHeaders[key]);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (source.postData.text) {
|
|
36
|
-
code.push(1, '.setBody(%s)', JSON.stringify(source.postData.text));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
code.push(1, '.execute()');
|
|
40
|
-
code.push(1, '.toCompletableFuture()');
|
|
41
|
-
code.push(1, '.thenAccept(System.out::println)');
|
|
42
|
-
code.push(1, '.join();');
|
|
43
|
-
code.blank();
|
|
44
|
-
code.push('client.close();');
|
|
45
|
-
|
|
46
|
-
return code.join();
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
module.exports.info = {
|
|
50
|
-
key: 'asynchttp',
|
|
51
|
-
title: 'AsyncHttp',
|
|
52
|
-
link: 'https://github.com/AsyncHttpClient/async-http-client',
|
|
53
|
-
description: 'Asynchronous Http and WebSocket Client library for Java',
|
|
54
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for Java using java.net.http.
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @wtetsu
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
|
|
13
|
-
module.exports = function (source, options) {
|
|
14
|
-
const opts = {
|
|
15
|
-
indent: ' ',
|
|
16
|
-
...options,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const code = new CodeBuilder(opts.indent);
|
|
20
|
-
|
|
21
|
-
code.push('HttpRequest request = HttpRequest.newBuilder()');
|
|
22
|
-
code.push(2, '.uri(URI.create("%s"))', source.fullUrl);
|
|
23
|
-
|
|
24
|
-
const headers = Object.keys(source.allHeaders);
|
|
25
|
-
|
|
26
|
-
// construct headers
|
|
27
|
-
if (headers.length) {
|
|
28
|
-
headers.forEach(function (key) {
|
|
29
|
-
code.push(2, '.header("%s", "%s")', key, source.allHeaders[key]);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (source.postData.text) {
|
|
34
|
-
code.push(
|
|
35
|
-
2,
|
|
36
|
-
'.method("%s", HttpRequest.BodyPublishers.ofString(%s))',
|
|
37
|
-
source.method.toUpperCase(),
|
|
38
|
-
JSON.stringify(source.postData.text)
|
|
39
|
-
);
|
|
40
|
-
} else {
|
|
41
|
-
code.push(2, '.method("%s", HttpRequest.BodyPublishers.noBody())', source.method.toUpperCase());
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
code.push(2, '.build();');
|
|
45
|
-
|
|
46
|
-
code.push(
|
|
47
|
-
'HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());'
|
|
48
|
-
);
|
|
49
|
-
code.push('System.out.println(response.body());');
|
|
50
|
-
|
|
51
|
-
return code.join();
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
module.exports.info = {
|
|
55
|
-
key: 'nethttp',
|
|
56
|
-
title: 'java.net.http',
|
|
57
|
-
link: 'https://openjdk.java.net/groups/net/httpclient/intro.html',
|
|
58
|
-
description: 'Java Standardized HTTP Client API',
|
|
59
|
-
};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @description
|
|
3
|
-
* HTTP code snippet generator for Java using OkHttp.
|
|
4
|
-
*
|
|
5
|
-
* @author
|
|
6
|
-
* @shashiranjan84
|
|
7
|
-
*
|
|
8
|
-
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
-
|
|
13
|
-
module.exports = function (source, options) {
|
|
14
|
-
const opts = {
|
|
15
|
-
indent: ' ',
|
|
16
|
-
...options,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const code = new CodeBuilder(opts.indent);
|
|
20
|
-
|
|
21
|
-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
|
|
22
|
-
|
|
23
|
-
const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'];
|
|
24
|
-
|
|
25
|
-
code.push('OkHttpClient client = new OkHttpClient();').blank();
|
|
26
|
-
|
|
27
|
-
if (source.postData.text) {
|
|
28
|
-
if (source.postData.boundary) {
|
|
29
|
-
code.push(
|
|
30
|
-
'MediaType mediaType = MediaType.parse("%s; boundary=%s");',
|
|
31
|
-
source.postData.mimeType,
|
|
32
|
-
source.postData.boundary
|
|
33
|
-
);
|
|
34
|
-
} else {
|
|
35
|
-
code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType);
|
|
36
|
-
}
|
|
37
|
-
code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
code.push('Request request = new Request.Builder()');
|
|
41
|
-
code.push(1, '.url("%s")', source.fullUrl);
|
|
42
|
-
if (methods.indexOf(source.method.toUpperCase()) === -1) {
|
|
43
|
-
if (source.postData.text) {
|
|
44
|
-
code.push(1, '.method("%s", body)', source.method.toUpperCase());
|
|
45
|
-
} else {
|
|
46
|
-
code.push(1, '.method("%s", null)', source.method.toUpperCase());
|
|
47
|
-
}
|
|
48
|
-
} else if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
|
|
49
|
-
if (source.postData.text) {
|
|
50
|
-
code.push(1, '.%s(body)', source.method.toLowerCase());
|
|
51
|
-
} else {
|
|
52
|
-
code.push(1, '.%s(null)', source.method.toLowerCase());
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
code.push(1, '.%s()', source.method.toLowerCase());
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Add headers, including the cookies
|
|
59
|
-
const headers = Object.keys(source.allHeaders);
|
|
60
|
-
|
|
61
|
-
// construct headers
|
|
62
|
-
if (headers.length) {
|
|
63
|
-
headers.forEach(function (key) {
|
|
64
|
-
code.push(1, '.addHeader("%s", "%s")', key, source.allHeaders[key]);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
code.push(1, '.build();').blank().push('Response response = client.newCall(request).execute();');
|
|
69
|
-
|
|
70
|
-
return code.join();
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
module.exports.info = {
|
|
74
|
-
key: 'okhttp',
|
|
75
|
-
title: 'OkHttp',
|
|
76
|
-
link: 'http://square.github.io/okhttp/',
|
|
77
|
-
description: 'An HTTP Request Client Library',
|
|
78
|
-
};
|