1. function decodeHttpRequestInput(req) {
  2. let decodedData = {};
  3. // Handle query parameters
  4. if (req && req.query) {
  5. for (const key in req.query) {
  6. if (req.query.hasOwnProperty(key)) {
  7. const encodedValue = decodeURIComponent(req.query[key]); // Decode URL-encoded values
  8. decodedData[key] = encodedValue;
  9. }
  10. }
  11. }
  12. // Handle request body (assuming JSON)
  13. if (req && req.body) {
  14. try {
  15. const parsedBody = JSON.parse(req.body);
  16. decodedData = { ...decodedData, ...parsedBody }; // Merge with existing data
  17. } catch (error) {
  18. // Handle JSON parsing errors gracefully
  19. console.error("Error parsing JSON body:", error);
  20. // Optionally, add a specific error key to the decoded data
  21. decodedData.error = "Invalid JSON body";
  22. }
  23. }
  24. // Handle form data (assuming multipart/form-data)
  25. if (req && req.files) {
  26. for (const file in req.files) {
  27. if (req.files.hasOwnProperty(file)) {
  28. const fileData = req.files[file];
  29. //You can process fileData here, e.g., read its content
  30. decodedData[file] = fileData;
  31. }
  32. }
  33. }
  34. return decodedData;
  35. }

Add your comment