Skip to content

Commit 5fb6d78

Browse files
committed
fix: multipart/form-data header issues with node/js fetch targets
1 parent 66baca6 commit 5fb6d78

File tree

8 files changed

+26
-24
lines changed

8 files changed

+26
-24
lines changed

src/targets/javascript/fetch/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import stringifyObject from 'stringify-object';
1212

1313
import { CodeBuilder } from '../../../helpers/code-builder';
14+
import { getHeaderName } from '../../../helpers/headers';
1415
import { Client } from '../../targets';
1516

1617
interface FetchOptions {
@@ -37,6 +38,15 @@ export const fetch: Client<FetchOptions> = {
3738
method,
3839
};
3940

41+
// The FormData API automatically adds a `Content-Type` header for `multipart/form-data`
42+
// content and if we add our own here data won't be correctly transmitted.
43+
if (postData.mimeType === 'multipart/form-data') {
44+
const contentTypeHeader = getHeaderName(allHeaders, 'content-type');
45+
if (contentTypeHeader) {
46+
delete allHeaders[contentTypeHeader];
47+
}
48+
}
49+
4050
if (Object.keys(allHeaders).length) {
4151
options.headers = allHeaders;
4252
}

src/targets/javascript/fetch/fixtures/multipart-data.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const form = new FormData();
22
form.append('foo', 'Hello World');
33

4-
const options = {
5-
method: 'POST',
6-
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
7-
};
4+
const options = {method: 'POST'};
85

96
options.body = form;
107

src/targets/javascript/fetch/fixtures/multipart-file.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const form = new FormData();
22
form.append('foo', 'test/fixtures/files/hello.txt');
33

4-
const options = {
5-
method: 'POST',
6-
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
7-
};
4+
const options = {method: 'POST'};
85

96
options.body = form;
107

src/targets/javascript/fetch/fixtures/multipart-form-data.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const form = new FormData();
22
form.append('foo', 'bar');
33

4-
const options = {
5-
method: 'POST',
6-
headers: {'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001'}
7-
};
4+
const options = {method: 'POST'};
85

96
options.body = form;
107

src/targets/node/fetch/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import stringifyObject from 'stringify-object';
1212

1313
import { CodeBuilder } from '../../../helpers/code-builder';
14+
import { getHeaderName } from '../../../helpers/headers';
1415
import { Client } from '../../targets';
1516

1617
export const fetch: Client = {
@@ -35,6 +36,15 @@ export const fetch: Client = {
3536
method,
3637
};
3738

39+
// The `form-data` module automatically adds a `Content-Type` header for `multipart/form-data`
40+
// content and if we add our own here data won't be correctly transmitted.
41+
if (postData.mimeType === 'multipart/form-data') {
42+
const contentTypeHeader = getHeaderName(headersObj, 'content-type');
43+
if (contentTypeHeader) {
44+
delete headersObj[contentTypeHeader];
45+
}
46+
}
47+
3848
if (Object.keys(headersObj).length) {
3949
reqOpts.headers = headersObj;
4050
}

src/targets/node/fetch/fixtures/multipart-data.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ formData.append('foo', fs.createReadStream('hello.txt'));
77

88
let url = 'http://mockbin.com/har';
99

10-
let options = {
11-
method: 'POST',
12-
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
13-
};
10+
let options = {method: 'POST'};
1411

1512
options.body = formData;
1613

src/targets/node/fetch/fixtures/multipart-file.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ formData.append('foo', fs.createReadStream('test/fixtures/files/hello.txt'));
77

88
let url = 'http://mockbin.com/har';
99

10-
let options = {
11-
method: 'POST',
12-
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
13-
};
10+
let options = {method: 'POST'};
1411

1512
options.body = formData;
1613

src/targets/node/fetch/fixtures/multipart-form-data.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ formData.append('foo', 'bar');
66

77
let url = 'http://mockbin.com/har';
88

9-
let options = {
10-
method: 'POST',
11-
headers: {'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001'}
12-
};
9+
let options = {method: 'POST'};
1310

1411
options.body = formData;
1512

0 commit comments

Comments
 (0)