mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(fetch-adapter): set correct Content-Type for Node FormData (#6998)
* fix(fetch-adapter): set correct Content-Type for Node FormData * Update lib/helpers/resolveConfig.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * test(fetch): replace chai expect with Node assert * fix: define formHeaders for FormData to resolve no-undef error * fix: filter headers to only update the target headers Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
committed by
GitHub
parent
066b39195a
commit
a9f47afbf3
@@ -23,15 +23,19 @@ export default (config) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let contentType;
|
|
||||||
|
|
||||||
if (utils.isFormData(data)) {
|
if (utils.isFormData(data)) {
|
||||||
if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
|
if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
|
||||||
headers.setContentType(undefined); // Let the browser set it
|
headers.setContentType(undefined); // browser handles it
|
||||||
} else if ((contentType = headers.getContentType()) !== false) {
|
} else if (utils.isFunction(data.getHeaders)) {
|
||||||
// fix semicolon duplication issue for ReactNative FormData implementation
|
// Node.js FormData (like form-data package)
|
||||||
const [type, ...tokens] = contentType ? contentType.split(';').map(token => token.trim()).filter(Boolean) : [];
|
const formHeaders = data.getHeaders();
|
||||||
headers.setContentType([type || 'multipart/form-data', ...tokens].join('; '));
|
// Only set safe headers to avoid overwriting security headers
|
||||||
|
const allowedHeaders = ['content-type', 'content-length'];
|
||||||
|
Object.entries(formHeaders).forEach(([key, val]) => {
|
||||||
|
if (allowedHeaders.includes(key.toLowerCase())) {
|
||||||
|
headers.set(key, val);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -393,4 +393,20 @@ describe('supports fetch with nodejs', function () {
|
|||||||
|
|
||||||
assert.strictEqual(headers.get('foo'), 'bar');
|
assert.strictEqual(headers.get('foo'), 'bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('fetch adapter - Content-Type handling', function () {
|
||||||
|
it('should set correct Content-Type for FormData automatically', async function () {
|
||||||
|
const FormData = (await import('form-data')).default; // Node FormData
|
||||||
|
const form = new FormData();
|
||||||
|
form.append('foo', 'bar');
|
||||||
|
|
||||||
|
server = await startHTTPServer((req, res) => {
|
||||||
|
const contentType = req.headers['content-type'];
|
||||||
|
assert.match(contentType, /^multipart\/form-data; boundary=/i);
|
||||||
|
res.end('OK');
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchAxios.post('/form', form);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user