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.
Why Use Path Parameters?
- 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! π±- 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
Accessing the Value
Path Parameter Types
String (default)
Integer
UUID
Boolean
Multiple Path Parameters
Practical Examples
Example 1: Products API
Example 2: Blog with Slugs
Example 3: Complete RESTful API
Example 4: Nested Resources
Combining with Other Parameters
Route Precedence
Validation and Errors
Incorrect Type
Creating Specific Error Endpoints
Best Practices
β
Do
β Avoid
Troubleshooting
Problem: "404 Not Found" when it should work
Problem: Template {{path-params.id}} is not replaced
{{path-params.id}} is not replacedProblem: Parameter comes as string when wanted number
Next Steps
See Also
Last updated
Was this helpful?