Skip to content

Commit 7e746c4

Browse files
authored
fix(curl) remove boundary form content-type header (#227)
1 parent 71eec07 commit 7e746c4

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/targets/shell/curl.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
const util = require('util')
1414
const helpers = require('../../helpers/shell')
15+
const headerHelpers = require('../../helpers/headers')
1516
const CodeBuilder = require('../../helpers/code-builder')
1617

1718
module.exports = function (source, options) {
@@ -39,6 +40,22 @@ module.exports = function (source, options) {
3940
code.push(opts.short ? '-0' : '--http1.0')
4041
}
4142

43+
// if multipart form data, we want to remove the boundary
44+
if (source.postData.mimeType === 'multipart/form-data') {
45+
const contentTypeHeaderName = headerHelpers.getHeaderName(source.headersObj, 'content-type')
46+
const contentTypeHeader = source.headersObj[contentTypeHeaderName]
47+
48+
if (contentTypeHeaderName && contentTypeHeader) {
49+
// remove the leading semi colon and boundary
50+
// up to the next semi colon or the end of string
51+
const noBoundary = contentTypeHeader.replace(/; boundary.+?(?=(;|$))/, '')
52+
53+
// replace the content-type header with no boundary in both headersObj and allHeaders
54+
source.headersObj[contentTypeHeaderName] = noBoundary
55+
source.allHeaders[contentTypeHeaderName] = noBoundary
56+
}
57+
}
58+
4259
// construct headers
4360
Object.keys(source.headersObj).sort().forEach(function (key) {
4461
const header = util.format('%s: %s', key, source.headersObj[key])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
curl --request POST \
22
--url http://mockbin.com/har \
3-
--header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
3+
--header 'content-type: multipart/form-data' \
44
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
curl --request POST \
22
--url http://mockbin.com/har \
3-
--header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
3+
--header 'content-type: multipart/form-data' \
44
--form foo=@test/fixtures/files/hello.txt
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
curl --request POST \
22
--url http://mockbin.com/har \
3-
--header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \
3+
--header 'Content-Type: multipart/form-data' \
44
--form foo=bar

0 commit comments

Comments
 (0)