diff --git a/codegens/curl/lib/index.js b/codegens/curl/lib/index.js index a951a802f..e8ef3d293 100644 --- a/codegens/curl/lib/index.js +++ b/codegens/curl/lib/index.js @@ -141,7 +141,7 @@ self = module.exports = { 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); - snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`; + snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType, true)}${quoteType}`; break; } diff --git a/codegens/curl/test/unit/convert.test.js b/codegens/curl/test/unit/convert.test.js index bcef07029..8b492f34e 100644 --- a/codegens/curl/test/unit/convert.test.js +++ b/codegens/curl/test/unit/convert.test.js @@ -34,6 +34,41 @@ describe('curl convert function', function () { }); }); + it('should escape backslash in raw bodies', function () { + request = new sdk.Request({ + 'method': 'POST', + 'header': [ + { + 'key': 'Content-Type', + 'value': 'application/json' + } + ], + 'body': { + 'mode': 'raw', + 'raw': '{ "foo": "\\" }' + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + + expect(snippet).to.contain('{ "foo": "\\\\" }'); + }); + }); + it('should return snippet with url in single quote(\')', function () { request = new sdk.Request({ 'method': 'POST',