Skip to content

Commit 22c5bf0

Browse files
committed
In Dart codegen, use json.convert for application/json body
1 parent 8799e9b commit 22c5bf0

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

codegens/dart-http/lib/index.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,19 @@ function parseUrlEncoded (body, indent, trim) {
3030
*
3131
* @param {Object} body Raw body data
3232
* @param {Boolean} trim indicates whether to trim string or not
33+
* @param {String} contentType the content-type of request body
3334
*/
34-
function parseRawBody (body, trim) {
35+
function parseRawBody (body, trim, contentType) {
36+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
37+
try {
38+
let jsonBody = JSON.parse(body);
39+
return `request.body = json.encode(${JSON.stringify(jsonBody, null, 4)});`;
40+
41+
}
42+
catch (error) {
43+
// Do nothing
44+
}
45+
}
3546
return `request.body = '''${sanitize(body, trim)}''';`;
3647
}
3748

@@ -109,15 +120,16 @@ function parseFormData (body, indent, trim) {
109120
*
110121
* @param {Object} body body object from request.
111122
* @param {String} indent indentation required for code snippet
112-
* @param {trim} trim indicates whether to trim string or not
123+
* @param {Boolean} trim indicates whether to trim string or not
124+
* @param {String} contentType the content-type of the request body
113125
*/
114-
function parseBody (body, indent, trim) {
126+
function parseBody (body, indent, trim, contentType) {
115127
if (!_.isEmpty(body)) {
116128
switch (body.mode) {
117129
case 'urlencoded':
118130
return parseUrlEncoded(body.urlencoded, indent, trim);
119131
case 'raw':
120-
return parseRawBody(body.raw, trim);
132+
return parseRawBody(body.raw, trim, contentType);
121133
case 'formdata':
122134
return parseFormData(body.formdata, indent, trim);
123135
case 'graphql':
@@ -167,13 +179,10 @@ self = module.exports = {
167179
footerSnippet = '',
168180
trim,
169181
timeout,
170-
followRedirect;
182+
followRedirect,
183+
contentType;
171184
options = sanitizeOptions(options, self.getOptions());
172-
if (options.includeBoilerplate) {
173-
headerSnippet = 'import \'package:http/http.dart\' as http;\n\n';
174-
headerSnippet += 'void main() async {\n';
175-
footerSnippet = '}\n';
176-
}
185+
177186
trim = options.trimRequestBody;
178187
indent = options.indentType === 'Tab' ? '\t' : ' ';
179188
indent = indent.repeat(options.indentCount);
@@ -199,6 +208,16 @@ self = module.exports = {
199208
}
200209
}
201210

211+
contentType = request.headers.get('Content-Type');
212+
if (options.includeBoilerplate) {
213+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
214+
headerSnippet = 'import \'dart:convert\';\n';
215+
}
216+
headerSnippet += 'import \'package:http/http.dart\' as http;\n\n';
217+
headerSnippet += 'void main() async {\n';
218+
footerSnippet = '}\n';
219+
}
220+
202221
// The following code handles multiple files in the same formdata param.
203222
// It removes the form data params where the src property is an array of filepath strings
204223
// Splits that array into different form data params with src set as a single filepath string
@@ -243,7 +262,7 @@ self = module.exports = {
243262

244263
const headers = parseHeaders(request.headers.toJSON(), indent, trim),
245264
requestBody = request.body ? request.body.toJSON() : {},
246-
body = parseBody(requestBody, indent, trim) + '\n';
265+
body = parseBody(requestBody, indent, trim, contentType) + '\n';
247266

248267
codeSnippet += headers;
249268

0 commit comments

Comments
 (0)