Path Parameters
Learn how to use path parameters (URL) in moclojer to create dynamic endpoints that respond to different ID, slug, and other data values.
Path parameters allow you to create dynamic endpoints that respond to different values in the URL. For example, a single endpoint /users/:id can respond to both /users/1 and /users/999.
Why Use Path Parameters?
Before (without path params):
- endpoint:
path: /users/1
response:
body: '{"id": 1, "name": "Alice"}'
- endpoint:
path: /users/2
response:
body: '{"id": 2, "name": "Bob"}'
# ... you would need 1000 endpoints for 1000 users! π±After (with path params):
- endpoint:
path: /users/:id
response:
body: >
{
"id": "{{path-params.id}}",
"name": "User {{path-params.id}}"
}
# A single endpoint responds to ANY ID! πBasic Syntax
Declaring a Path Parameter
Use a colon (:) before the parameter name:
Format: /path/:parameterName
Accessing the Value
Use templates {{path-params.parameterName}}:
Test:
Path Parameter Types
Moclojer supports type validation using the :param|type syntax:
String (default)
Matches:
/users/aliceβ/users/bob123β/users/JoΓ£oβ
Integer
Matches:
/users/1β/users/999β/users/0β
Doesn't match:
/users/abcβ/users/1.5β/users/β
UUID
Matches:
/sessions/550e8400-e29b-41d4-a716-446655440000β
Doesn't match:
/sessions/abc123β/sessions/123β
Boolean
Matches:
/features/trueβ/features/falseβ
Doesn't match:
/features/yesβ/features/1β
Multiple Path Parameters
You can have multiple parameters in the same path:
Test:
Response:
Practical Examples
Example 1: Products API
Usage:
Example 2: Blog with Slugs
Usage:
Example 3: Complete RESTful API
Example 4: Nested Resources
Usage:
Combining with Other Parameters
Path parameters work together with query params and body params:
Usage:
Response:
Route Precedence
When multiple endpoints can match, moclojer uses the first occurrence:
How it works:
GET /users/meβ matches endpoint #1 βGET /users/123β matches endpoint #2 β
If you reverse the order:
Result:
GET /users/meβ matches endpoint #1 (:id= "me") β Wrong!
Golden rule: Specific routes before dynamic routes!
Validation and Errors
Incorrect Type
If you define a type and the value doesn't match:
Requests:
/users/123β β Match/users/abcβ β No match (moclojer returns 404)
Creating Specific Error Endpoints
Best Practices
β
Do
Use explicit types when possible
Descriptive parameter names
Specific routes before dynamic ones
Use parameter value in response
β Avoid
Parameters without type when should have one
Names too generic
Too many nesting levels
Troubleshooting
Problem: "404 Not Found" when it should work
Possible causes:
Incorrect parameter type
Wrong route order
Different HTTP method
Problem: Template {{path-params.id}} is not replaced
{{path-params.id}} is not replacedCause: Parameter name doesn't match
Problem: Parameter comes as string when wanted number
Cause: Template strings always return strings
Note: Without quotes = number, with quotes = string.
Next Steps
Now that you've mastered path parameters:
Query Parameters - Parameters in the URL after
?Body Parameters - Data in the request body
Template Variables - Complete template reference
See Also
HTTP Methods - GET, POST, PUT, DELETE, etc.
Path Patterns - Advanced route patterns
Dynamic Responses Tutorial - Practical tutorial
Last updated
Was this helpful?