@@ -30,8 +30,20 @@ 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
34+ * @param {Integer } indentCount the number of space to use
3335 */
34- function parseRawBody ( body , trim ) {
36+ function parseRawBody ( body , trim , contentType , indentCount ) {
37+ if ( contentType && ( contentType === 'application/json' || contentType . match ( / \+ j s o n $ / ) ) ) {
38+ try {
39+ let jsonBody = JSON . parse ( body ) ;
40+ return `request.body = json.encode(${ JSON . stringify ( jsonBody , null , indentCount ) } );` ;
41+
42+ }
43+ catch ( error ) {
44+ // Do nothing
45+ }
46+ }
3547 return `request.body = '''${ sanitize ( body , trim ) } ''';` ;
3648}
3749
@@ -109,15 +121,16 @@ function parseFormData (body, indent, trim) {
109121 *
110122 * @param {Object } body body object from request.
111123 * @param {String } indent indentation required for code snippet
112- * @param {trim } trim indicates whether to trim string or not
124+ * @param {Boolean } trim indicates whether to trim string or not
125+ * @param {String } contentType the content-type of the request body
113126 */
114- function parseBody ( body , indent , trim ) {
127+ function parseBody ( body , indent , trim , contentType ) {
115128 if ( ! _ . isEmpty ( body ) ) {
116129 switch ( body . mode ) {
117130 case 'urlencoded' :
118131 return parseUrlEncoded ( body . urlencoded , indent , trim ) ;
119132 case 'raw' :
120- return parseRawBody ( body . raw , trim ) ;
133+ return parseRawBody ( body . raw , trim , contentType , indent . length ) ;
121134 case 'formdata' :
122135 return parseFormData ( body . formdata , indent , trim ) ;
123136 case 'graphql' :
@@ -167,13 +180,10 @@ self = module.exports = {
167180 footerSnippet = '' ,
168181 trim ,
169182 timeout ,
170- followRedirect ;
183+ followRedirect ,
184+ contentType ;
171185 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- }
186+
177187 trim = options . trimRequestBody ;
178188 indent = options . indentType === 'Tab' ? '\t' : ' ' ;
179189 indent = indent . repeat ( options . indentCount ) ;
@@ -199,6 +209,16 @@ self = module.exports = {
199209 }
200210 }
201211
212+ contentType = request . headers . get ( 'Content-Type' ) ;
213+ if ( options . includeBoilerplate ) {
214+ if ( contentType && ( contentType === 'application/json' || contentType . match ( / \+ j s o n $ / ) ) ) {
215+ headerSnippet = 'import \'dart:convert\';\n' ;
216+ }
217+ headerSnippet += 'import \'package:http/http.dart\' as http;\n\n' ;
218+ headerSnippet += 'void main() async {\n' ;
219+ footerSnippet = '}\n' ;
220+ }
221+
202222 // The following code handles multiple files in the same formdata param.
203223 // It removes the form data params where the src property is an array of filepath strings
204224 // Splits that array into different form data params with src set as a single filepath string
@@ -243,7 +263,7 @@ self = module.exports = {
243263
244264 const headers = parseHeaders ( request . headers . toJSON ( ) , indent , trim ) ,
245265 requestBody = request . body ? request . body . toJSON ( ) : { } ,
246- body = parseBody ( requestBody , indent , trim ) + '\n' ;
266+ body = parseBody ( requestBody , indent , trim , contentType ) + '\n' ;
247267
248268 codeSnippet += headers ;
249269
0 commit comments