Problem Description:
I am using the AI Agent in n8n to collect user input and send it to the HTTP Request node. However, when I map the output from the AI Agent to {{$fromAI('json')}}, the HTTP Request node fails with errors.
Sometimes the request receives an empty body (query: {}) instead of the expected JSON.
If I manually input data, the HTTP Request node works fine.
Authorization (Bearer Token) is properly set in the headers.
Tried using {{$json}} instead of $fromAI(‘json’), but still facing issues.
I see that your AI Agent’s output is not properly mapping to the HTTP Request node , which sometimes causes an empty request body (query: {}) . Here’s how to fix it:
1. Ensure the AI Agent Outputs Proper JSON
Modify the System Message in the AI Agent to force structured output:
When responding, always return valid JSON. Example format:
{
“query”: {
“key1”: “value1”,
“key2”: “value2”
}
}
Do not include extra text, explanations, or formatting outside this JSON structure.
• Then test with:
Example input: “Generate a request for user details”
• The AI must return structured JSON, not plain text.
2. Update the JSON Mapping in the HTTP Request Node
Instead of:
{{$fromAI(‘json’)}}
Use:
{{$json[“query”] || “{}”}}
• This ensures a fallback value ({}) if query is missing.
3. Debug the AI Output Before the HTTP Request
• Add a Function node between the AI Agent and HTTP request.
• Inside the Function node, use:
return [{ “debug”: JSON.stringify($json) }];
• Run the workflow and check the debug output to confirm the AI sends valid JSON.
Try these fixes and let me know if the issue persists!