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}}
{{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
Use JSON for modern APIs
Validate Content-Type in client
Echo received data
Use numbers without quotes
Error endpoints for validation
β Avoid
GET with body
Sensitive data in GET
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
Path Parameters - Parameters in the URL
Query Parameters - Filters and pagination
Header Parameters - HTTP headers
HTTP Methods - POST, PUT, PATCH
See Also
CRUD Operations - Complete examples
Template Variables - Complete reference
Dynamic Responses - Practical tutorial
Last updated
Was this helpful?