How to make a HTTP Request POST or PATCH with Body Params

Hi,

I’m wondering how to make a HTTP Request “dynamic”, with POST or PATCH behaviour.

I define the request method with a variable, like the URL.

{
  "nodes": [
    {
      "parameters": {
        "requestMethod": "= {{$json[\"method\"]}}",
        "url": "={{$env[\"MY_URL\"]}}",
        "jsonParameters": true,
        "options": {}
      },
      "name": "Dynamic HTTP POST / PATCH",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1050,
        350
      ],
      "alwaysOutputData": true
    }
  ],
  "connections": {}
}

But, the HTTP Request node don’t really know it could only be a POST or PATCH request.

So, only the Headers / Query params are displayed.

Same if JSON/Raw Parameters are enabled (that sounds consistent :wink: )

image

Could we let the user define what exactly he want to send ?

Does it sounds okay for you ?

Thanks for your answer

That will not work because depending on the selected method different parameters will be displayed.

So if you want to do something like that, you have to use an IF or Switch Node and then direct to different HTTP Request Nodes.

Thanks for your quick answer, again !

I tested with an IF, but I’m stucked cause the IF don’t pass always in both true / false branches.

I used a merge after, but it’s not trigerred when one of the both branches is not used.

I hack by coding directly in a Function node.

If you know how to do with a Merge by the way, I’m curious (if my explanations are enough understandable).

Yes, I think I know what you mean. That in the case the IF-Node does not output anything on true the Merge-Node will not execute at all.

What you can do here is to set “Always Ouput Data” on the IF-Node and then filer out the empty items after the merge.

Here an example workflow:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      number: 1\n    }\n  },\n  {\n    json: {\n      number: 1\n    }\n  }\n];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json[\"number\"]}}",
              "operation": "equal"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {},
      "name": "GoOn",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1250,
        260
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{Object.keys($json).length}}",
              "operation": "notEqual"
            }
          ]
        }
      },
      "name": "Filter Empty",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "Filter Empty",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Filter Empty": {
      "main": [
        [
          {
            "node": "GoOn",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like