File tree 2 files changed +58
-3
lines changed
2 files changed +58
-3
lines changed Original file line number Diff line number Diff line change @@ -151,9 +151,20 @@ self = module.exports = {
151
151
let rawBody = body . raw . toString ( ) ,
152
152
isAsperandPresent = _ . includes ( rawBody , '@' ) ,
153
153
// Use the long option if `@` is present in the request body otherwise follow user setting
154
- optionName = isAsperandPresent ? '--data-raw' : form ( '-d' , format ) ;
155
- // eslint-disable-next-line max-len
156
- snippet += indent + `${ optionName } ${ quoteType } ${ sanitize ( rawBody , trim , quoteType ) } ${ quoteType } ` ;
154
+ optionName = isAsperandPresent ? '--data-raw' : form ( '-d' , format ) ,
155
+ sanitizedBody = sanitize ( rawBody , trim , quoteType ) ;
156
+
157
+ if ( ! multiLine ) {
158
+ try {
159
+ sanitizedBody = JSON . stringify ( JSON . parse ( sanitizedBody ) ) ;
160
+ }
161
+ catch ( e ) {
162
+ // Do nothing
163
+ }
164
+ }
165
+
166
+ snippet += indent + `${ optionName } ${ quoteType } ${ sanitizedBody } ${ quoteType } ` ;
167
+
157
168
break ;
158
169
}
159
170
Original file line number Diff line number Diff line change @@ -289,6 +289,50 @@ describe('curl convert function', function () {
289
289
} ) ;
290
290
} ) ;
291
291
292
+ it ( 'should return snippet with JSON body in single line if multiline option is false' , function ( ) {
293
+ request = new Request ( {
294
+ 'method' : 'POST' ,
295
+ 'header' : [ ] ,
296
+ 'body' : {
297
+ 'mode' : 'raw' ,
298
+ 'raw' : '{\n "name": "John",\n "type": "names",\n "id": "123sdaw"\n}' ,
299
+ 'options' : {
300
+ 'raw' : {
301
+ 'language' : 'json'
302
+ }
303
+ }
304
+ } ,
305
+ 'url' : {
306
+ 'raw' : 'https://postman-echo.com/post' ,
307
+ 'protocol' : 'https' ,
308
+ 'host' : [
309
+ 'postman-echo' ,
310
+ 'com'
311
+ ] ,
312
+ 'path' : [
313
+ 'post'
314
+ ]
315
+ }
316
+ } ) ;
317
+ options = {
318
+ multiLine : false ,
319
+ longFormat : false ,
320
+ lineContinuationCharacter : '\\' ,
321
+ quoteType : 'single' ,
322
+ requestTimeoutInSeconds : 0 ,
323
+ followRedirect : true ,
324
+ followOriginalHttpMethod : false
325
+ } ;
326
+
327
+ convert ( request , options , function ( error , snippet ) {
328
+ if ( error ) {
329
+ expect . fail ( null , null , error ) ;
330
+ }
331
+ expect ( snippet ) . to . be . a ( 'string' ) ;
332
+ expect ( snippet ) . to . contain ( '-d \'{"name":"John","type":"names","id":"123sdaw"}\'' ) ;
333
+ } ) ;
334
+ } ) ;
335
+
292
336
it ( 'should return snippet with backslash(\\) character as line continuation ' +
293
337
'character for multiline code generation' , function ( ) {
294
338
request = new Request ( {
You can’t perform that action at this time.
0 commit comments