Solving HTTP Request Errors, JSON parameter needs to be valid JSON errors in n8n workflow, with JavaScript
Introduction
While working with n8n workflows, I encountered an issue with an HTTP Request node that resulted in a "Bad Request" error and sometimes "n8n problem in node http request json parameter needs to be valid json". The problem was identified as invalid JSON parameters being passed to the node. This blog post details the issue and the JavaScript solution I implemented to resolve it.
The Problem
The error message:
Problem in node ‘HTTP Request‘
Bad request - please check your parameters
Problem in node ‘HTTP Request‘
json parameter needs to be valid jsonProblem in node ‘AI Agent‘
No prompt specified
\n
) and bullet points (*
) causing parsing issues. Sometimes the problem arises even try with "Output Parser Node" and error shows. If you have face any of the above errors you simple need to remove the "Output Parser Node" and add a "Code Node" with the given JavaScript code in the below, and remember to modify it as per your requirements.The Solution
To fix this, I added a Code Node with a JavaScript code snippet between the AI Agent message and the HTTP Request node. The code cleans the input text by removing newline characters and bullet points, then formats it into valid JSON. Here’s the code I used:
// Get the input text from the 'state' field
const inputText = $input.first().json.output;
// Remove \n and clean up bullet points
const cleanedText = inputText.replace(/\n/g, " ").replace(/\*/g, " ");
// Return valid JSON for Telegram
return [{
json: {
text: cleanedText
}
}];
Get the code from the link : Code Node
How It Works
- The code retrieves the input text from the
state
field using$input.first().json.output
. - It replaces all newline characters (
\n
) and bullet points (*
) with spaces to ensure the text is clean. - The cleaned text is then wrapped in a valid JSON object and returned in the required format for the HTTP Request node.
Conclusion
By implementing this simple JavaScript solution, the "Bad Request" error was resolved, allowing the workflow to successfully send messages via Facebook Messenger. This approach can be adapted for other scenarios where text cleaning and valid JSON formatting are necessary in n8n workflows.
Comments
Post a Comment