From 3e9954de82393a4db891784d5ffe916ab7a9a854 Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Fri, 16 Jun 2023 11:05:15 -0400 Subject: [PATCH 1/5] first cut at supporting restsharp 107+ --- src/targets/csharp/restsharp/client.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/targets/csharp/restsharp/client.ts b/src/targets/csharp/restsharp/client.ts index 15f2a6e83..2d36789d3 100644 --- a/src/targets/csharp/restsharp/client.ts +++ b/src/targets/csharp/restsharp/client.ts @@ -3,6 +3,10 @@ import { escapeForDoubleQuotes } from '../../../helpers/escape'; import { getHeader } from '../../../helpers/headers'; import { Client } from '../../targets'; +function title(s: string): string { + return s[0].toUpperCase() + s.slice(1).toLowerCase(); +} + export const restsharp: Client = { info: { key: 'restsharp', @@ -10,7 +14,7 @@ export const restsharp: Client = { link: 'http://restsharp.org/', description: 'Simple REST and HTTP API Client for .NET', }, - convert: ({ allHeaders, method, fullUrl, headersObj, cookies, postData }) => { + convert: ({ allHeaders, method, fullUrl, headersObj, cookies, postData, uriObj }) => { const { push, join } = new CodeBuilder(); const isSupportedMethod = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'].includes( method.toUpperCase(), @@ -20,26 +24,32 @@ export const restsharp: Client = { return 'Method not supported'; } - push(`var client = new RestClient("${fullUrl}");`); - push(`var request = new RestRequest(Method.${method.toUpperCase()});`); + push(`var options = new RestClientOptions("${fullUrl}");`); + push(`var client = new RestClient(options);`); - // Add headers, including the cookies + // The first argument is the sub-path to the base URL, given as the + // constructor to RestClient; for our purposes we're just giving the entire + // URL as the base path so it can be an empty string + push(`var request = new RestRequest("");`); + // Add headers, including the cookies Object.keys(headersObj).forEach(key => { push(`request.AddHeader("${key}", "${escapeForDoubleQuotes(headersObj[key])}");`); }); cookies.forEach(({ name, value }) => { - push(`request.AddCookie("${name}", "${value}");`); + push(`request.AddCookie("${name}", "${value}", "${uriObj.pathname}", "${uriObj.host}");`); }); + // here we're just assuming that the text is a JSON post, and that content + // type is application/json. Improvements welcome to support multipart or + // form-urlencoded if (postData.text) { - const header = getHeader(allHeaders, 'content-type'); const text = JSON.stringify(postData.text); - push(`request.AddParameter("${header}", ${text}, ParameterType.RequestBody);`); + push(`request.AddJsonBody(${text}, false);`); } - push('IRestResponse response = client.Execute(request);'); + push(`var response = await client.${title(method)}Async(request);`); return join(); }, }; From f9da2080d6be645cbf4582288eddc27cfef4a3a9 Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Fri, 16 Jun 2023 11:08:43 -0400 Subject: [PATCH 2/5] update fixtures --- .../restsharp/fixtures/application-form-encoded.cs | 9 +++++---- .../csharp/restsharp/fixtures/application-json.cs | 9 +++++---- src/targets/csharp/restsharp/fixtures/cookies.cs | 11 ++++++----- src/targets/csharp/restsharp/fixtures/full.cs | 13 +++++++------ src/targets/csharp/restsharp/fixtures/headers.cs | 7 ++++--- src/targets/csharp/restsharp/fixtures/https.cs | 7 ++++--- .../csharp/restsharp/fixtures/jsonObj-multiline.cs | 9 +++++---- .../csharp/restsharp/fixtures/jsonObj-null-value.cs | 9 +++++---- .../csharp/restsharp/fixtures/multipart-data.cs | 9 +++++---- .../csharp/restsharp/fixtures/multipart-file.cs | 9 +++++---- .../fixtures/multipart-form-data-no-params.cs | 7 ++++--- .../restsharp/fixtures/multipart-form-data.cs | 9 +++++---- src/targets/csharp/restsharp/fixtures/nested.cs | 7 ++++--- src/targets/csharp/restsharp/fixtures/query.cs | 7 ++++--- src/targets/csharp/restsharp/fixtures/short.cs | 7 ++++--- src/targets/csharp/restsharp/fixtures/text-plain.cs | 9 +++++---- 16 files changed, 77 insertions(+), 61 deletions(-) diff --git a/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs b/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs index 4e4744d01..d1db972da 100644 --- a/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs +++ b/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("content-type", "application/x-www-form-urlencoded"); -request.AddParameter("application/x-www-form-urlencoded", "foo=bar&hello=world", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("foo=bar&hello=world", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/application-json.cs b/src/targets/csharp/restsharp/fixtures/application-json.cs index 2ee8c46f1..2db6f6fd6 100644 --- a/src/targets/csharp/restsharp/fixtures/application-json.cs +++ b/src/targets/csharp/restsharp/fixtures/application-json.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("content-type", "application/json"); -request.AddParameter("application/json", "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/cookies.cs b/src/targets/csharp/restsharp/fixtures/cookies.cs index f0489b650..7086ce03e 100644 --- a/src/targets/csharp/restsharp/fixtures/cookies.cs +++ b/src/targets/csharp/restsharp/fixtures/cookies.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); -request.AddCookie("foo", "bar"); -request.AddCookie("bar", "baz"); -IRestResponse response = client.Execute(request); \ No newline at end of file +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); +request.AddCookie("foo", "bar", "/har", "mockbin.com"); +request.AddCookie("bar", "baz", "/har", "mockbin.com"); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/full.cs b/src/targets/csharp/restsharp/fixtures/full.cs index 14b52d976..a8c5d2a4b 100644 --- a/src/targets/csharp/restsharp/fixtures/full.cs +++ b/src/targets/csharp/restsharp/fixtures/full.cs @@ -1,8 +1,9 @@ -var client = new RestClient("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("accept", "application/json"); request.AddHeader("content-type", "application/x-www-form-urlencoded"); -request.AddCookie("foo", "bar"); -request.AddCookie("bar", "baz"); -request.AddParameter("application/x-www-form-urlencoded", "foo=bar", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddCookie("foo", "bar", "/har", "mockbin.com"); +request.AddCookie("bar", "baz", "/har", "mockbin.com"); +request.AddJsonBody("foo=bar", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/headers.cs b/src/targets/csharp/restsharp/fixtures/headers.cs index 9a02c6db5..c86b3076f 100644 --- a/src/targets/csharp/restsharp/fixtures/headers.cs +++ b/src/targets/csharp/restsharp/fixtures/headers.cs @@ -1,6 +1,7 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.GET); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("accept", "application/json"); request.AddHeader("x-foo", "Bar"); request.AddHeader("quoted-value", "\"quoted\" 'string'"); -IRestResponse response = client.Execute(request); \ No newline at end of file +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/https.cs b/src/targets/csharp/restsharp/fixtures/https.cs index 05a369073..7ba860f13 100644 --- a/src/targets/csharp/restsharp/fixtures/https.cs +++ b/src/targets/csharp/restsharp/fixtures/https.cs @@ -1,3 +1,4 @@ -var client = new RestClient("https://mockbin.com/har"); -var request = new RestRequest(Method.GET); -IRestResponse response = client.Execute(request); \ No newline at end of file +var options = new RestClientOptions("https://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs b/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs index 14fbe7778..33d084517 100644 --- a/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs +++ b/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("content-type", "application/json"); -request.AddParameter("application/json", "{\n \"foo\": \"bar\"\n}", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("{\n \"foo\": \"bar\"\n}", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs b/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs index 36b092c94..e6046bc4e 100644 --- a/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs +++ b/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("content-type", "application/json"); -request.AddParameter("application/json", "{\"foo\":null}", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("{\"foo\":null}", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-data.cs b/src/targets/csharp/restsharp/fixtures/multipart-data.cs index 7c95fe632..0dff1df7f 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-data.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-data.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001"); -request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-file.cs b/src/targets/csharp/restsharp/fixtures/multipart-file.cs index 47758542c..8b750a67c 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-file.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-file.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001"); -request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs b/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs index be2fcac1f..3ff491bd1 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs @@ -1,4 +1,5 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("Content-Type", "multipart/form-data"); -IRestResponse response = client.Execute(request); \ No newline at end of file +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs b/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs index 9eb6893b6..412ca3955 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("Content-Type", "multipart/form-data; boundary=---011000010111000001101001"); -request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n", false); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/nested.cs b/src/targets/csharp/restsharp/fixtures/nested.cs index e28bf3190..9ce76fae6 100644 --- a/src/targets/csharp/restsharp/fixtures/nested.cs +++ b/src/targets/csharp/restsharp/fixtures/nested.cs @@ -1,3 +1,4 @@ -var client = new RestClient("http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value"); -var request = new RestRequest(Method.GET); -IRestResponse response = client.Execute(request); \ No newline at end of file +var options = new RestClientOptions("http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value"); +var client = new RestClient(options); +var request = new RestRequest(""); +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/query.cs b/src/targets/csharp/restsharp/fixtures/query.cs index 3e3112c02..e76fd9751 100644 --- a/src/targets/csharp/restsharp/fixtures/query.cs +++ b/src/targets/csharp/restsharp/fixtures/query.cs @@ -1,3 +1,4 @@ -var client = new RestClient("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"); -var request = new RestRequest(Method.GET); -IRestResponse response = client.Execute(request); \ No newline at end of file +var options = new RestClientOptions("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"); +var client = new RestClient(options); +var request = new RestRequest(""); +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/short.cs b/src/targets/csharp/restsharp/fixtures/short.cs index b644539c4..f3039ba87 100644 --- a/src/targets/csharp/restsharp/fixtures/short.cs +++ b/src/targets/csharp/restsharp/fixtures/short.cs @@ -1,3 +1,4 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.GET); -IRestResponse response = client.Execute(request); \ No newline at end of file +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/text-plain.cs b/src/targets/csharp/restsharp/fixtures/text-plain.cs index a0a672de6..da9c41f77 100644 --- a/src/targets/csharp/restsharp/fixtures/text-plain.cs +++ b/src/targets/csharp/restsharp/fixtures/text-plain.cs @@ -1,5 +1,6 @@ -var client = new RestClient("http://mockbin.com/har"); -var request = new RestRequest(Method.POST); +var options = new RestClientOptions("http://mockbin.com/har"); +var client = new RestClient(options); +var request = new RestRequest(""); request.AddHeader("content-type", "text/plain"); -request.AddParameter("text/plain", "Hello World", ParameterType.RequestBody); -IRestResponse response = client.Execute(request); \ No newline at end of file +request.AddJsonBody("Hello World", false); +var response = await client.PostAsync(request); \ No newline at end of file From cae680c894fc9e8e8a6729fdf05f0dced8f9b1ce Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Fri, 16 Jun 2023 12:13:56 -0400 Subject: [PATCH 3/5] handle mime types properly --- src/targets/csharp/restsharp/client.ts | 49 ++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/targets/csharp/restsharp/client.ts b/src/targets/csharp/restsharp/client.ts index 2d36789d3..f3cf1404b 100644 --- a/src/targets/csharp/restsharp/client.ts +++ b/src/targets/csharp/restsharp/client.ts @@ -32,8 +32,23 @@ export const restsharp: Client = { // URL as the base path so it can be an empty string push(`var request = new RestRequest("");`); + // If we have multipart form data, set this value. Setting the content-type header manually and then trying to add a mutlipart file parameter + const isMultipart = postData.mimeType && postData.mimeType == 'multipart/form-data'; + if (isMultipart) { + push(`request.AlwaysMultipartFormData = true;`); + } + // Add headers, including the cookies Object.keys(headersObj).forEach(key => { + // if we have post data, restsharp really wants to set the contentType + // itself; do not add a content-type header or you end up with failures + // which manifest as unhandled exceptions. + if (postData.mimeType && key.toLowerCase() == 'content-type') { + if (isMultipart && postData.boundary) { + push(`request.FormBoundary = "${postData.boundary}";`); + } + return; + } push(`request.AddHeader("${key}", "${escapeForDoubleQuotes(headersObj[key])}");`); }); @@ -41,15 +56,35 @@ export const restsharp: Client = { push(`request.AddCookie("${name}", "${value}", "${uriObj.pathname}", "${uriObj.host}");`); }); - // here we're just assuming that the text is a JSON post, and that content - // type is application/json. Improvements welcome to support multipart or - // form-urlencoded - if (postData.text) { - const text = JSON.stringify(postData.text); - push(`request.AddJsonBody(${text}, false);`); + switch (postData.mimeType) { + case 'multipart/form-data': + if (!postData.params) break; + postData.params.forEach(param => { + if (param.fileName) { + push(`request.AddFile("${param.name}", "${param.fileName}");`); + } else { + push(`request.AddParameter("${param.name}", "${param.value}");`); + } + }); + break; + case 'application/x-www-form-urlencoded': + if (!postData.params) break; + postData.params.forEach(param => { + push(`request.AddParameter("${param.name}", "${param.value}");`); + }); + break; + case 'application/json': { + if (!postData.text) break; + const text = JSON.stringify(postData.text); + push(`request.AddJsonBody(${text}, false);`); + break; + } + default: + if (!postData.text) break; + push(`request.AddStringBody("${postData.text}", "${postData.mimeType}");`); } - push(`var response = await client.${title(method)}Async(request);`); + push(`var response = await client.${title(method)}Async(request); `); return join(); }, }; From a5e179f47bc780017a6f673bc6c762ef3a4a1703 Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Fri, 16 Jun 2023 12:14:13 -0400 Subject: [PATCH 4/5] update fixtures --- .../csharp/restsharp/fixtures/application-form-encoded.cs | 6 +++--- src/targets/csharp/restsharp/fixtures/application-json.cs | 3 +-- src/targets/csharp/restsharp/fixtures/cookies.cs | 2 +- src/targets/csharp/restsharp/fixtures/full.cs | 5 ++--- src/targets/csharp/restsharp/fixtures/headers.cs | 2 +- src/targets/csharp/restsharp/fixtures/https.cs | 2 +- .../csharp/restsharp/fixtures/jsonObj-multiline.cs | 3 +-- .../csharp/restsharp/fixtures/jsonObj-null-value.cs | 3 +-- src/targets/csharp/restsharp/fixtures/multipart-data.cs | 8 +++++--- src/targets/csharp/restsharp/fixtures/multipart-file.cs | 7 ++++--- .../restsharp/fixtures/multipart-form-data-no-params.cs | 4 ++-- .../csharp/restsharp/fixtures/multipart-form-data.cs | 7 ++++--- src/targets/csharp/restsharp/fixtures/nested.cs | 2 +- src/targets/csharp/restsharp/fixtures/query.cs | 2 +- src/targets/csharp/restsharp/fixtures/short.cs | 2 +- src/targets/csharp/restsharp/fixtures/text-plain.cs | 5 ++--- 16 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs b/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs index d1db972da..47c612555 100644 --- a/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs +++ b/src/targets/csharp/restsharp/fixtures/application-form-encoded.cs @@ -1,6 +1,6 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("content-type", "application/x-www-form-urlencoded"); -request.AddJsonBody("foo=bar&hello=world", false); -var response = await client.PostAsync(request); \ No newline at end of file +request.AddParameter("foo", "bar"); +request.AddParameter("hello", "world"); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/application-json.cs b/src/targets/csharp/restsharp/fixtures/application-json.cs index 2db6f6fd6..0a6428235 100644 --- a/src/targets/csharp/restsharp/fixtures/application-json.cs +++ b/src/targets/csharp/restsharp/fixtures/application-json.cs @@ -1,6 +1,5 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("content-type", "application/json"); request.AddJsonBody("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}", false); -var response = await client.PostAsync(request); \ No newline at end of file +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/cookies.cs b/src/targets/csharp/restsharp/fixtures/cookies.cs index 7086ce03e..6f9a97396 100644 --- a/src/targets/csharp/restsharp/fixtures/cookies.cs +++ b/src/targets/csharp/restsharp/fixtures/cookies.cs @@ -3,4 +3,4 @@ var request = new RestRequest(""); request.AddCookie("foo", "bar", "/har", "mockbin.com"); request.AddCookie("bar", "baz", "/har", "mockbin.com"); -var response = await client.PostAsync(request); \ No newline at end of file +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/full.cs b/src/targets/csharp/restsharp/fixtures/full.cs index a8c5d2a4b..fe7f93531 100644 --- a/src/targets/csharp/restsharp/fixtures/full.cs +++ b/src/targets/csharp/restsharp/fixtures/full.cs @@ -2,8 +2,7 @@ var client = new RestClient(options); var request = new RestRequest(""); request.AddHeader("accept", "application/json"); -request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddCookie("foo", "bar", "/har", "mockbin.com"); request.AddCookie("bar", "baz", "/har", "mockbin.com"); -request.AddJsonBody("foo=bar", false); -var response = await client.PostAsync(request); \ No newline at end of file +request.AddParameter("foo", "bar"); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/headers.cs b/src/targets/csharp/restsharp/fixtures/headers.cs index c86b3076f..c583d6a70 100644 --- a/src/targets/csharp/restsharp/fixtures/headers.cs +++ b/src/targets/csharp/restsharp/fixtures/headers.cs @@ -4,4 +4,4 @@ request.AddHeader("accept", "application/json"); request.AddHeader("x-foo", "Bar"); request.AddHeader("quoted-value", "\"quoted\" 'string'"); -var response = await client.GetAsync(request); \ No newline at end of file +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/https.cs b/src/targets/csharp/restsharp/fixtures/https.cs index 7ba860f13..5687a5cf7 100644 --- a/src/targets/csharp/restsharp/fixtures/https.cs +++ b/src/targets/csharp/restsharp/fixtures/https.cs @@ -1,4 +1,4 @@ var options = new RestClientOptions("https://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -var response = await client.GetAsync(request); \ No newline at end of file +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs b/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs index 33d084517..95d6423c6 100644 --- a/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs +++ b/src/targets/csharp/restsharp/fixtures/jsonObj-multiline.cs @@ -1,6 +1,5 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("content-type", "application/json"); request.AddJsonBody("{\n \"foo\": \"bar\"\n}", false); -var response = await client.PostAsync(request); \ No newline at end of file +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs b/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs index e6046bc4e..006d15bcd 100644 --- a/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs +++ b/src/targets/csharp/restsharp/fixtures/jsonObj-null-value.cs @@ -1,6 +1,5 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("content-type", "application/json"); request.AddJsonBody("{\"foo\":null}", false); -var response = await client.PostAsync(request); \ No newline at end of file +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-data.cs b/src/targets/csharp/restsharp/fixtures/multipart-data.cs index 0dff1df7f..96711bf74 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-data.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-data.cs @@ -1,6 +1,8 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001"); -request.AddJsonBody("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"bar\"\r\n\r\nBonjour le monde\r\n-----011000010111000001101001--\r\n", false); -var response = await client.PostAsync(request); \ No newline at end of file +request.AlwaysMultipartFormData = true; +request.FormBoundary = "---011000010111000001101001"; +request.AddFile("foo", "hello.txt"); +request.AddParameter("bar", "Bonjour le monde"); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-file.cs b/src/targets/csharp/restsharp/fixtures/multipart-file.cs index 8b750a67c..f096a1c30 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-file.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-file.cs @@ -1,6 +1,7 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001"); -request.AddJsonBody("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n", false); -var response = await client.PostAsync(request); \ No newline at end of file +request.AlwaysMultipartFormData = true; +request.FormBoundary = "---011000010111000001101001"; +request.AddFile("foo", "test/fixtures/files/hello.txt"); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs b/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs index 3ff491bd1..3d9768098 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-form-data-no-params.cs @@ -1,5 +1,5 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("Content-Type", "multipart/form-data"); -var response = await client.PostAsync(request); \ No newline at end of file +request.AlwaysMultipartFormData = true; +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs b/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs index 412ca3955..e0fa46214 100644 --- a/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs +++ b/src/targets/csharp/restsharp/fixtures/multipart-form-data.cs @@ -1,6 +1,7 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("Content-Type", "multipart/form-data; boundary=---011000010111000001101001"); -request.AddJsonBody("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n", false); -var response = await client.PostAsync(request); \ No newline at end of file +request.AlwaysMultipartFormData = true; +request.FormBoundary = "---011000010111000001101001"; +request.AddParameter("foo", "bar"); +var response = await client.PostAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/nested.cs b/src/targets/csharp/restsharp/fixtures/nested.cs index 9ce76fae6..0015ecd98 100644 --- a/src/targets/csharp/restsharp/fixtures/nested.cs +++ b/src/targets/csharp/restsharp/fixtures/nested.cs @@ -1,4 +1,4 @@ var options = new RestClientOptions("http://mockbin.com/har?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value"); var client = new RestClient(options); var request = new RestRequest(""); -var response = await client.GetAsync(request); \ No newline at end of file +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/query.cs b/src/targets/csharp/restsharp/fixtures/query.cs index e76fd9751..5eb0cbc4d 100644 --- a/src/targets/csharp/restsharp/fixtures/query.cs +++ b/src/targets/csharp/restsharp/fixtures/query.cs @@ -1,4 +1,4 @@ var options = new RestClientOptions("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"); var client = new RestClient(options); var request = new RestRequest(""); -var response = await client.GetAsync(request); \ No newline at end of file +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/short.cs b/src/targets/csharp/restsharp/fixtures/short.cs index f3039ba87..8b314c475 100644 --- a/src/targets/csharp/restsharp/fixtures/short.cs +++ b/src/targets/csharp/restsharp/fixtures/short.cs @@ -1,4 +1,4 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -var response = await client.GetAsync(request); \ No newline at end of file +var response = await client.GetAsync(request); \ No newline at end of file diff --git a/src/targets/csharp/restsharp/fixtures/text-plain.cs b/src/targets/csharp/restsharp/fixtures/text-plain.cs index da9c41f77..d0fdf2c60 100644 --- a/src/targets/csharp/restsharp/fixtures/text-plain.cs +++ b/src/targets/csharp/restsharp/fixtures/text-plain.cs @@ -1,6 +1,5 @@ var options = new RestClientOptions("http://mockbin.com/har"); var client = new RestClient(options); var request = new RestRequest(""); -request.AddHeader("content-type", "text/plain"); -request.AddJsonBody("Hello World", false); -var response = await client.PostAsync(request); \ No newline at end of file +request.AddStringBody("Hello World", "text/plain"); +var response = await client.PostAsync(request); \ No newline at end of file From ad3ff30d09bfb160fc90ae7d200cf880aea29f72 Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Fri, 16 Jun 2023 12:33:51 -0400 Subject: [PATCH 5/5] clean up unused --- src/targets/csharp/restsharp/client.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/targets/csharp/restsharp/client.ts b/src/targets/csharp/restsharp/client.ts index f3cf1404b..0e3e3566f 100644 --- a/src/targets/csharp/restsharp/client.ts +++ b/src/targets/csharp/restsharp/client.ts @@ -1,6 +1,5 @@ import { CodeBuilder } from '../../../helpers/code-builder'; import { escapeForDoubleQuotes } from '../../../helpers/escape'; -import { getHeader } from '../../../helpers/headers'; import { Client } from '../../targets'; function title(s: string): string { @@ -14,7 +13,7 @@ export const restsharp: Client = { link: 'http://restsharp.org/', description: 'Simple REST and HTTP API Client for .NET', }, - convert: ({ allHeaders, method, fullUrl, headersObj, cookies, postData, uriObj }) => { + convert: ({ method, fullUrl, headersObj, cookies, postData, uriObj }) => { const { push, join } = new CodeBuilder(); const isSupportedMethod = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'].includes( method.toUpperCase(),