Skip to content

Commit d0156cf

Browse files
author
Ankit Saini
authored
Escape backslash character in raw bodies for curl codegen (#674)
* Escape backslash character in raw bodies for curl codegen * Remove console log
1 parent ae8a681 commit d0156cf

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

codegens/curl/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ self = module.exports = {
141141
isAsperandPresent = _.includes(rawBody, '@'),
142142
// Use the long option if `@` is present in the request body otherwise follow user setting
143143
optionName = isAsperandPresent ? '--data-raw' : form('-d', format);
144-
snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`;
144+
snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType, true)}${quoteType}`;
145145
break;
146146
}
147147

codegens/curl/test/unit/convert.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,41 @@ describe('curl convert function', function () {
3434
});
3535
});
3636

37+
it('should escape backslash in raw bodies', function () {
38+
request = new sdk.Request({
39+
'method': 'POST',
40+
'header': [
41+
{
42+
'key': 'Content-Type',
43+
'value': 'application/json'
44+
}
45+
],
46+
'body': {
47+
'mode': 'raw',
48+
'raw': '{ "foo": "\\" }'
49+
},
50+
'url': {
51+
'raw': 'https://postman-echo.com/post',
52+
'protocol': 'https',
53+
'host': [
54+
'postman-echo',
55+
'com'
56+
],
57+
'path': [
58+
'post'
59+
]
60+
}
61+
});
62+
63+
convert(request, {}, function (error, snippet) {
64+
if (error) {
65+
expect.fail(null, null, error);
66+
}
67+
68+
expect(snippet).to.contain('{ "foo": "\\\\" }');
69+
});
70+
});
71+
3772
it('should return snippet with url in single quote(\')', function () {
3873
request = new sdk.Request({
3974
'method': 'POST',

0 commit comments

Comments
 (0)