🐛 Fix body formatting for x-form-www-urlencoded (#2269)

This commit is contained in:
Omar Ajoue 2021-10-01 17:43:50 +02:00 committed by GitHub
parent 5c206a3da5
commit 3253a81318
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -118,6 +118,16 @@ const createFormDataObject = (data: object) => {
return formData; return formData;
}; };
function searchForHeader(headers: IDataObject, headerName: string) {
if (headers === undefined) {
return undefined;
}
const headerNames = Object.keys(headers);
headerName = headerName.toLowerCase();
return headerNames.find((thisHeader) => thisHeader.toLowerCase() === headerName);
}
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
@ -183,13 +193,15 @@ async function parseRequestObject(requestObject: IDataObject) {
// 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) {
// If we have only form // If we have only form
axiosConfig.data = new URLSearchParams(requestObject.form as Record<string, string>); axiosConfig.data =
typeof requestObject.form === 'string'
? stringify(requestObject.form, { format: 'RFC3986' })
: stringify(requestObject.form).toString();
if (axiosConfig.headers !== undefined) { if (axiosConfig.headers !== undefined) {
// remove possibly existing content-type headers const headerName = searchForHeader(axiosConfig.headers, 'content-type');
const headers = Object.keys(axiosConfig.headers); if (headerName) {
headers.forEach((header) => delete axiosConfig.headers[headerName];
header.toLowerCase() === 'content-type' ? delete axiosConfig.headers[header] : null, }
);
axiosConfig.headers['Content-Type'] = 'application/x-www-form-urlencoded'; axiosConfig.headers['Content-Type'] = 'application/x-www-form-urlencoded';
} else { } else {
axiosConfig.headers = { axiosConfig.headers = {
@ -432,16 +444,6 @@ async function proxyRequestToAxios(
}); });
} }
function searchForHeader(headers: IDataObject, headerName: string) {
if (headers === undefined) {
return undefined;
}
const headerNames = Object.keys(headers);
headerName = headerName.toLowerCase();
return headerNames.find((thisHeader) => thisHeader.toLowerCase() === headerName);
}
function convertN8nRequestToAxios(n8nRequest: IHttpRequestOptions): AxiosRequestConfig { function convertN8nRequestToAxios(n8nRequest: IHttpRequestOptions): AxiosRequestConfig {
// Destructure properties with the same name first. // Destructure properties with the same name first.
const { headers, method, timeout, auth, proxy, url } = n8nRequest; const { headers, method, timeout, auth, proxy, url } = n8nRequest;