diff --git a/codegens/curl/lib/index.js b/codegens/curl/lib/index.js index d55ad5550..af57ca4d3 100644 --- a/codegens/curl/lib/index.js +++ b/codegens/curl/lib/index.js @@ -151,9 +151,20 @@ self = module.exports = { let rawBody = body.raw.toString(), isAsperandPresent = _.includes(rawBody, '@'), // Use the long option if `@` is present in the request body otherwise follow user setting - optionName = isAsperandPresent ? '--data-raw' : form('-d', format); - // eslint-disable-next-line max-len - snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`; + optionName = isAsperandPresent ? '--data-raw' : form('-d', format), + sanitizedBody = sanitize(rawBody, trim, quoteType); + + if (!multiLine) { + try { + sanitizedBody = JSON.stringify(JSON.parse(sanitizedBody)); + } + catch (e) { + // Do nothing + } + } + + snippet += indent + `${optionName} ${quoteType}${sanitizedBody}${quoteType}`; + break; } diff --git a/codegens/curl/test/unit/convert.test.js b/codegens/curl/test/unit/convert.test.js index ae2111acc..d3c4c5061 100644 --- a/codegens/curl/test/unit/convert.test.js +++ b/codegens/curl/test/unit/convert.test.js @@ -289,6 +289,50 @@ describe('curl convert function', function () { }); }); + it('should return snippet with JSON body in single line if multiline option is false', function () { + request = new Request({ + 'method': 'POST', + 'header': [], + 'body': { + 'mode': 'raw', + 'raw': '{\n "name": "John",\n "type": "names",\n "id": "123sdaw"\n}', + 'options': { + 'raw': { + 'language': 'json' + } + } + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + options = { + multiLine: false, + longFormat: false, + lineContinuationCharacter: '\\', + quoteType: 'single', + requestTimeoutInSeconds: 0, + followRedirect: true, + followOriginalHttpMethod: false + }; + + convert(request, options, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.contain('-d \'{"name":"John","type":"names","id":"123sdaw"}\''); + }); + }); + it('should return snippet with backslash(\\) character as line continuation ' + 'character for multiline code generation', function () { request = new Request({