Skip to content

Commit df6bd3e

Browse files
authored
Merge pull request #808 from dashrathky01/fix/update-postman-cli-codegen-form-data-handling
Fix multipart/form-data codegen missing --form flag for file fields
2 parents 60741ad + 6a1ee57 commit df6bd3e

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

codegens/postman-cli/lib/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,15 @@ function addFormDataBody (snippet, body, opts) {
217217
}
218218

219219
if (data.type === 'file') {
220-
const sanitizedSrc = sanitize(data.src, opts.trim, '"', true),
221-
wrappedSrc = `@"${sanitizedSrc}"`,
222-
finalSrc = sanitize(wrappedSrc, opts.trim, opts.quoteType, opts.quoteType === '"');
223-
snippet += ` ${opts.quoteType}${sanitize(data.key, opts.trim, opts.quoteType)}=${finalSrc}`;
220+
const sanitizedSrc = sanitize(data.src, opts.trim, opts.quoteType);
221+
snippet += `${opts.indent}${form('-f', opts.format)} ` +
222+
`${opts.quoteType}${sanitize(data.key, opts.trim, opts.quoteType)}=@${sanitizedSrc}`;
224223
snippet += opts.quoteType;
225224
}
226225
else {
227226
const sanitizedValue = sanitize(data.value, opts.trim, '"', true),
228227
finalValue = sanitize(sanitizedValue, opts.trim, opts.quoteType, opts.quoteType === '"');
229-
snippet += `${opts.indent} ${form('-f', opts.format)} ` +
228+
snippet += `${opts.indent}${form('-f', opts.format)} ` +
230229
`${opts.quoteType}${sanitize(data.key, opts.trim, opts.quoteType)}=${finalValue}`;
231230
snippet += opts.quoteType;
232231
}

codegens/postman-cli/test/unit/convert.test.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,54 @@ describe('postman-cli convert function', function () {
479479
expect.fail(null, null, error);
480480
}
481481
expect(snippet).to.be.a('string');
482-
expect(snippet).to.include('\'no file=@"/path/to/file"\'');
483-
expect(snippet).to.include('\'no src=@"/path/to/file"\'');
484-
expect(snippet).to.include('\'invalid src=@"/path/to/file"\'');
482+
expect(snippet).to.include('--form \'no file=@/path/to/file\'');
483+
expect(snippet).to.include('--form \'no src=@/path/to/file\'');
484+
expect(snippet).to.include('--form \'invalid src=@/path/to/file\'');
485+
});
486+
});
487+
488+
it('should generate correct --form flag for each field in multipart/form-data with text and file', function () {
489+
var request = new Request({
490+
'method': 'POST',
491+
'header': [],
492+
'body': {
493+
'mode': 'formdata',
494+
'formdata': [
495+
{
496+
'key': 'textField',
497+
'value': '123',
498+
'type': 'text'
499+
},
500+
{
501+
'key': 'fileField',
502+
'value': '',
503+
'type': 'file',
504+
'src': '/path/to/document.pdf'
505+
}
506+
]
507+
},
508+
'url': {
509+
'raw': 'https://postman-echo.com/post',
510+
'protocol': 'https',
511+
'host': [
512+
'postman-echo',
513+
'com'
514+
],
515+
'path': [
516+
'post'
517+
]
518+
}
519+
});
520+
convert(request, {}, function (error, snippet) {
521+
if (error) {
522+
expect.fail(null, null, error);
523+
}
524+
expect(snippet).to.be.a('string');
525+
// Each field should have its own --form flag
526+
expect(snippet).to.include('--form \'textField=');
527+
expect(snippet).to.include('--form \'fileField=@/path/to/document.pdf\'');
528+
// File path should NOT have inner double quotes
529+
expect(snippet).to.not.include('@"/path/to/document.pdf"');
485530
});
486531
});
487532

0 commit comments

Comments
 (0)