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:
- Stop force-converting every request payload to FormData
- Fix
formatDownloadRequestV2so 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 parameterconst 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;};