JSON parameter needs to be valid JSON [item 1]

Hello all,

I’m trying to setup an HTTP node in which I have a body in JSON but I always get the error that the JSON parameter needs to be a valid JSON.

Here is the body:

{
  "model": "Qubico/flux1-dev",
  "task_type": "txt2img",
  "input": {
    "prompt": "{{ $json.message.content }}",
    "width": 540,
    "height": 960
  }
} 

The strange thing is that in the result of that expression (when the message content is replaced), if I run the node with that then it works…

Any clue ?

Thanks in advance for your help

You must change the Body field to full Expression mode:
1- In the HTTP node, in the Body Parameters section, change from “JSON” to “RAW Parameters (JSON)”.
2- In the Body field, click the gear icon and select “Add Expression”.
3- Write the full expression as a valid JavaScript string:

return {
model: "Qubico/flux1-dev",
task_type: "txt2img",
input: {
prompt: $json.message.content,
width: 540,
height: 960
}
}

It is important not to use quotes inside the prompt, since if it is inside a JS object, it is already a string if the value is.

Thanks but I still have an error 400 with your solution. I guest in the content type I’m supposed to write application/json, right ?

Small detail, I’m hosting n8n at home (not sure if it changes something but just because I don’t have the “RAW Parameters (JSON)” option and only RAW…).

Any other idea?

try

{
  "model": "Qubico/flux1-dev",
  "task_type": "txt2img",
  "input": {
    "prompt": {{ $json.message.content.toJsonString() }},
    "width": 540,
    "height": 960
  }
}
2 Likes
  1. Change the HTTP node to “RAW” in Body Content Type. In the Body field, select “Expression” and type:
return JSON.stringify({ 
model: "Quubico/flux1-dev", 
task_type: "txt2img", 
input: { 
prompt: $json.message.content, 
width: 540, 
height: 960 
}
});
  1. In the Headers tab, add: Content-Type → application/json

Thanks a lot for your help it works :wink:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.