mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 04:47:29 -08:00
🐛 Add function to calculate content-length when using multipart/form-data (#2427)
This commit is contained in:
parent
abdcb0836e
commit
15e64d1bc4
|
@ -135,6 +135,28 @@ function searchForHeader(headers: IDataObject, headerName: string) {
|
||||||
return headerNames.find((thisHeader) => thisHeader.toLowerCase() === headerName);
|
return headerNames.find((thisHeader) => thisHeader.toLowerCase() === headerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function generateContentLengthHeader(formData: FormData, headers: IDataObject) {
|
||||||
|
if (!formData || !formData.getLength) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const length = await new Promise((res, rej) => {
|
||||||
|
formData.getLength((error: Error | null, length: number) => {
|
||||||
|
if (error) {
|
||||||
|
rej(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res(length);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
headers = Object.assign(headers, {
|
||||||
|
'content-length': length,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error('Unable to calculate form data length', { error });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function parseRequestObject(requestObject: IDataObject) {
|
async function parseRequestObject(requestObject: IDataObject) {
|
||||||
// This function is a temporary implementation
|
// This function is a temporary implementation
|
||||||
// That translates all http requests done via
|
// That translates all http requests done via
|
||||||
|
@ -199,6 +221,7 @@ async function parseRequestObject(requestObject: IDataObject) {
|
||||||
delete axiosConfig.headers[contentTypeHeaderKeyName];
|
delete axiosConfig.headers[contentTypeHeaderKeyName];
|
||||||
const headers = axiosConfig.data.getHeaders();
|
const headers = axiosConfig.data.getHeaders();
|
||||||
axiosConfig.headers = Object.assign(axiosConfig.headers || {}, headers);
|
axiosConfig.headers = Object.assign(axiosConfig.headers || {}, headers);
|
||||||
|
await generateContentLengthHeader(axiosConfig.data, axiosConfig.headers);
|
||||||
} else {
|
} else {
|
||||||
// When using the `form` property it means the content should be x-www-form-urlencoded.
|
// When using the `form` property it means the content should be x-www-form-urlencoded.
|
||||||
if (requestObject.form !== undefined && requestObject.body === undefined) {
|
if (requestObject.form !== undefined && requestObject.body === undefined) {
|
||||||
|
@ -235,6 +258,7 @@ async function parseRequestObject(requestObject: IDataObject) {
|
||||||
// Mix in headers as FormData creates the boundary.
|
// Mix in headers as FormData creates the boundary.
|
||||||
const headers = axiosConfig.data.getHeaders();
|
const headers = axiosConfig.data.getHeaders();
|
||||||
axiosConfig.headers = Object.assign(axiosConfig.headers || {}, headers);
|
axiosConfig.headers = Object.assign(axiosConfig.headers || {}, headers);
|
||||||
|
await generateContentLengthHeader(axiosConfig.data, axiosConfig.headers);
|
||||||
} else if (requestObject.body !== undefined) {
|
} else if (requestObject.body !== undefined) {
|
||||||
// If we have body and possibly form
|
// If we have body and possibly form
|
||||||
if (requestObject.form !== undefined) {
|
if (requestObject.form !== undefined) {
|
||||||
|
|
Loading…
Reference in a new issue