Skip to content

Commit 3cba11c

Browse files
committed
Update parseRequest.js
create getAddStringBodyParam function
1 parent 3e62660 commit 3cba11c

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

codegens/csharp-restsharp/lib/parseRequest.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ function parseContentType (request) {
4141
return request.getHeaders({enabled: true, ignoreCase: true})['content-type'] || 'text/plain';
4242
}
4343

44+
/**
45+
* Generates a parameter using AddStringBody method
46+
*
47+
* @param {Object} requestBody - JSON object representing body of request
48+
* @param {string} dataFormat - the data format to use "DataFormat.Json" or "DataFormat.Xml"
49+
* @returns {String} snippet of the parameter generation
50+
*/
51+
function getAddStringBodyParam (requestBody, dataFormat) {
52+
return `var body = ${requestBody[requestBody.mode]
53+
.split('\n')
54+
.map((line) => { return '@"' + line.replace(/"/g, '""') + '"'; })
55+
.join(' + "\\n" +\n')};\n` +
56+
`request.AddStringBody(body, ${dataFormat});\n`;
57+
}
58+
4459
/**
4560
* Parses Raw data
4661
*
@@ -52,11 +67,10 @@ function parseRawBody (request, requestBody) {
5267
let bodySnippet = '',
5368
contentType = parseContentType(request);
5469
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
55-
bodySnippet = `var body = ${requestBody[requestBody.mode]
56-
.split('\n')
57-
.map((line) => { return '@"' + line.replace(/"/g, '""') + '"'; })
58-
.join(' + "\\n" +\n')};\n` +
59-
'request.AddStringBody(body, DataFormat.Json);\n';
70+
bodySnippet = getAddStringBodyParam(requestBody, 'DataFormat.Json');
71+
}
72+
else if (contentType && (contentType === 'text/xml' || contentType.match(/\+xml$/))) {
73+
bodySnippet = getAddStringBodyParam(requestBody, 'DataFormat.Xml');
6074
}
6175
else {
6276
bodySnippet = `var body = ${requestBody[requestBody.mode]

0 commit comments

Comments
 (0)