475 words
2 minutes
A Rant About Our Project's Download Request Wrapper

A couple of days ago a requirement came in: a page needed an export button to download a report. The backend originally exposed the endpoint as a GET, but the input parameters were likely to keep growing until they blew past the maximum URL length, so we switched it to a POST.

The moment we switched, the endpoint started throwing errors. Digging into it, I found the culprit: this project converts the payload of every download endpoint into FormData — and the backend had no intention of accepting that format.

Here’s how the download request gets assembled:

const download = (requestUrl, method, instanceName, data) => {
const formData = new FormData();
if (data.bodyParams) {
Object.keys(data.bodyParams).forEach((key) => {
formData.append(key, data.bodyParams[key]);
});
} else {
Object.keys(data).forEach((key) => {
formData.append(key, data[key]);
});
}
const req = {
url: formatDownloadRequestV2(requestUrl, data),
method: method.toUpperCase(),
data: formData,
headers: {
'Content-Type': 'multipart/form-data',
filename: 'utf-8',
},
responseType: 'blob',
baseURL: config[`${instanceName ?? 'base'}URL`],
};
};

While fixing that, I stumbled onto a second latent bug: when the request is a GET with parameters, formatDownloadRequestV2 simply drops them. Here’s the function:

const formatDownloadRequestV2 = (requestUrl, data) => {
// Assemble the request parameters
let url = requestUrl;
if (Object.prototype.hasOwnProperty.call(data, 'pathParams')) {
Object.keys(data.pathParams).forEach((key) => {
url = requestUrl.replace(`{${key}}`, data.pathParams[key]);
});
}
if (Object.prototype.hasOwnProperty.call(data, 'urlParams')) {
Object.keys(data.urlParams).forEach((key, index) => {
url = index === 0 ? (url += `?${key}=${data.urlParams[key]}`) : (url += `&${key}=${data.urlParams[key]}`);
});
}
if (Object.prototype.hasOwnProperty.call(data, 'bodyParams')) {
return requestUrl;
}
return url;
};

So, two things to fix:

  1. Stop force-converting every request payload to FormData
  2. Fix formatDownloadRequestV2 so it builds GET URLs properly

Fix #1 looks like this.

In our project, API definitions usually look like this:

// Anchor performance - export comparison data
downloadCompareDate: {
url: '/anchorPerformance/comparison/export',
method: 'POST',
instanceName: 'live',
isDownload: true,
},

First, add a noFormData flag to the API definition:

// Anchor performance - export comparison data
downloadCompareDate: {
noFormData: true,
},

Then update the download method:

// Add the noFormData parameter
const download = (requestUrl, method, instanceName, data, noFormData) => {
const req = {
url: formatDownloadRequestV2(requestUrl, data, method.toUpperCase()),
method: method.toUpperCase(),
// If no FormData conversion is needed, pass data through as-is
data: noFormData ? data : formData,
headers: {
'Content-Type': noFormData ? 'application/json' : 'multipart/form-data',
filename: 'utf-8',
},
responseType: 'blob',
baseURL: config[`${instanceName ?? 'base'}URL`],
};
};

And fix #2:

const formatDownloadRequestV2 = (requestUrl, data, method) => {
// Assemble the request parameters
let url = requestUrl;
// Track whether any of the three if branches below matched
let flag = true;
if (Object.prototype.hasOwnProperty.call(data, 'pathParams')) {
flag = false;
Object.keys(data.pathParams).forEach((key) => {
url = requestUrl.replace(`{${key}}`, data.pathParams[key]);
});
}
if (Object.prototype.hasOwnProperty.call(data, 'urlParams')) {
flag = false;
Object.keys(data.urlParams).forEach((key, index) => {
url = index === 0 ? (url += `?${key}=${data.urlParams[key]}`) : (url += `&${key}=${data.urlParams[key]}`);
});
}
if (Object.prototype.hasOwnProperty.call(data, 'bodyParams')) {
flag = false;
return requestUrl;
}
// Fix the URL for GET requests
if (flag && method === 'GET') {
url += formatURL(data);
}
return url;
};
A Rant About Our Project's Download Request Wrapper
https://www.mihouo.com/en/posts/front/rants-on-wrapping-download-requests-in-projects/
Author
Jia
Published at
2024-03-15
License
CC BY-NC-SA 4.0