function decodeHttpRequestInput(req) {
let decodedData = {};
// Handle query parameters
if (req && req.query) {
for (const key in req.query) {
if (req.query.hasOwnProperty(key)) {
const encodedValue = decodeURIComponent(req.query[key]); // Decode URL-encoded values
decodedData[key] = encodedValue;
}
}
}
// Handle request body (assuming JSON)
if (req && req.body) {
try {
const parsedBody = JSON.parse(req.body);
decodedData = { ...decodedData, ...parsedBody }; // Merge with existing data
} catch (error) {
// Handle JSON parsing errors gracefully
console.error("Error parsing JSON body:", error);
// Optionally, add a specific error key to the decoded data
decodedData.error = "Invalid JSON body";
}
}
// Handle form data (assuming multipart/form-data)
if (req && req.files) {
for (const file in req.files) {
if (req.files.hasOwnProperty(file)) {
const fileData = req.files[file];
//You can process fileData here, e.g., read its content
decodedData[file] = fileData;
}
}
}
return decodedData;
}
Add your comment