Body Parameters

Learn how to work with body parameters (JSON, form data) in moclojer. Access data from the request body in POST, PUT, and PATCH.

Body parameters are data sent in the body of HTTP requests, primarily in POST, PUT, and PATCH. Moclojer allows you to access this data via templates and use it in dynamic responses.

What Are Body Parameters?

Body is where you send complex data in HTTP requests:

# Example of POST with JSON in body
curl -X POST http://localhost:8000/users \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "[email protected]",
    "age": 30
  }'

Difference from other parameters:

  • Path params: Data in the URL (/users/:id)

  • Query params: Data after ? (?page=1)

  • Body params: Data in the request body (JSON, form data)

Why Use Body Parameters?

βœ… Complex data: Nested objects, arrays, multiple fields βœ… Security: Don't appear in URL (logs, history) βœ… Size: No URL limit (which is ~2KB) βœ… Structured: JSON allows hierarchies

When to use:

  • Create resources (POST)

  • Update resources (PUT, PATCH)

  • Operations with lots of data

  • Sensitive data (passwords, tokens)

When NOT to use:

  • GET requests (GET should not have body)

  • Simple DELETE (use path params)

  • Filters/pagination (use query params)


Accessing Body Parameters

Syntax: {{json-params.field}}

Test:

Response:


Content-Type: application/json

Simple JSON

Request:

Nested Objects

Request:

Response:

Arrays in Body

Request:

⚠️ Note: Templates don't iterate over arrays automatically. The array is returned as a string.


Data Types

Strings

Request: {"name": "Alice"} Response: {"name": "Alice"}

Numbers (no quotes!)

Request: {"age": 25, "price": 99.99} Response: {"age": 25, "price": 99.99}

⚠️ Important: No quotes for numbers! With quotes it becomes string.

Booleans

Request: {"completed": true, "active": false} Response: {"completed": true, "active": false}

Null

Request: {"deletedAt": null} Response: {"deletedAt": null}


Combining Parameters

Body + Path Parameters

Request:

Response:

Body + Query Parameters

Request:

Body + Headers

Request:


Practical Use Cases

1. Create User (POST)

Request:

2. Update Profile (PATCH)

Request:

3. Login (Authentication)

Request:

4. Create Order (E-commerce)

Request:

5. Upload Metadata (without binary file)

Request:


Validation and Errors

Required Fields (Simulation)

Moclojer does not validate automatically. Simulate with specific endpoints:

⚠️ Limitation: Moclojer doesn't validate if field exists. Use tools like Prism for real validation.

Invalid Types

Invalid Email


Content-Type: application/x-www-form-urlencoded

Moclojer supports form data (less common in modern APIs):

Request:

⚠️ Note: Use JSON when possible. Form-urlencoded has limitations (no nested objects).


Best Practices

βœ… Do

  1. Use JSON for modern APIs

  2. Validate Content-Type in client

  3. Echo received data

  4. Use numbers without quotes

  5. Error endpoints for validation

❌ Avoid

  1. GET with body

  2. Sensitive data in GET

  3. Very large body inline


Troubleshooting

Problem: Template is not replaced

Cause: Incorrect field name

Problem: Number comes as string

Cause: Quotes around template

Problem: Nested object doesn't work

Cause: Incorrect syntax

Problem: "Unexpected end of JSON"

Cause: Empty template breaks JSON


Next Steps

See Also

Last updated

Was this helpful?