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

  1. Use explicit types when possible

  2. Descriptive parameter names

  3. Specific routes before dynamic ones

  4. Use parameter value in response

❌ Avoid

  1. Parameters without type when should have one

  2. Names too generic

  3. Too many nesting levels

Troubleshooting

Problem: "404 Not Found" when it should work

Possible causes:

  1. Incorrect parameter type

  2. Wrong route order

  3. Different HTTP method

Problem: Template {{path-params.id}} is not replaced

Cause: 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:

  1. Query Parameters - Parameters in the URL after ?

  2. Body Parameters - Data in the request body

  3. Template Variables - Complete template reference

See Also

Last updated

Was this helpful?