vigneswaran_R:
I’m trying to set up an n8n webhook that can handle any request method (GET
, POST
, PUT
, etc.) and support dynamic URL parameters .
For example, I want the webhook to work for URLs like:
http://localhost:5678/webhook/api/gateway/user
http://localhost:5678/webhook/api/gateway/user/123
http://localhost:5678/webhook/api/gateway/user/123/status
http://localhost:5678/webhook/api/gateway/user/123/status/update/more
note:
The number of parameters in the URL is not fixed (it may vary).
1. Configure the Webhook Node
Add a Webhook Node in n8n.
Set the HTTP Method to *
(accepts any request method).
Define the Webhook Path :
Use a flexible base path, like:
/api/gateway/*
The *
allows capturing any subpath beyond /api/gateway/
.
Response Mode : On Received
2. Extract Dynamic URL Parameters
Since n8n does not natively parse arbitrary URL paths , you need to extract the path dynamically.
The Webhook Node will store the full request URL inside {{$json["headers"]["x-forwarded-path"]}}
.
Expression to Extract URL Parameters
Use a Set Node to split the URL path into an array:
{{(() => {
const fullPath = $json["headers"]["x-forwarded-path"] || "";
return fullPath.replace("/webhook/api/gateway/", "").split("/");
})()}}
This will convert:
/webhook/api/gateway/user/123/status/update/more
Into:
["user", "123", "status", "update", "more"]
3. Build a Workflow
This workflow: Accepts any HTTP method
Supports dynamic URL parameters
Returns structured response
Workflow Steps
Webhook Node (Capture the request)
Set Node (Extract URL parameters)
Respond to Webhook Node (Return the parsed parameters)
Full Workflow JSON
Here’s a ready-to-import n8n workflow:
{
"nodes": [
{
"parameters": {
"path": "api/gateway/*",
"httpMethod": "*",
"responseMode": "onReceived"
},
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 1,
"position": [
400,
300
]
},
{
"parameters": {
"values": {
"string": [
{
"name": "Method",
"value": "={{$json[\"headers\"][\"x-method\"]}}"
},
{
"name": "Full Path",
"value": "={{$json[\"headers\"][\"x-forwarded-path\"]}}"
},
{
"name": "URL Parameters",
"value": "={{(() => {
const fullPath = $json[\"headers\"][\"x-forwarded-path\"] || \"\";
return fullPath.replace(\"/webhook/api/gateway/\", \"\").split(\"/\");
})()}}"
}
]
}
},
"name": "Extract Parameters",
"type": "n8n-nodes-base.set",
"typeVersion": 1,
"position": [
600,
300
]
},
{
"parameters": {
"responseCode": 200,
"responseBody": "={{$json}}"
},
"name": "Respond to Webhook",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1,
"position": [
800,
300
]
}
],
"connections": {
"Webhook": {
"main": [
[
{
"node": "Extract Parameters",
"type": "main",
"index": 0
}
]
]
},
"Extract Parameters": {
"main": [
[
{
"node": "Respond to Webhook",
"type": "main",
"index": 0
}
]
]
}
}
}
4. Test the Webhook
Start the Workflow (Click “Execute Workflow”).
Send a request from Postman, cURL, or your browser:Examples:
GET http://localhost:5678/webhook/api/gateway/user
POST http://localhost:5678/webhook/api/gateway/user/123
PUT http://localhost:5678/webhook/api/gateway/user/123/status
DELETE http://localhost:5678/webhook/api/gateway/user/123/status/update/more
Response Example :
json
{
"Method": "GET",
"Full Path": "/webhook/api/gateway/user/123/status/update/more",
"URL Parameters": ["user", "123", "status", "update", "more"]
}
Try this, and write the test results.
Thanks