In this tutorial, you'll create a simple but functional mock API server from scratch. By the end, you'll have a working mock server that responds to HTTP requests with JSON data.
What you'll learn
How to write a basic moclojer configuration file
How to define HTTP endpoints
How to structure JSON responses
How to start and test your mock server
What you'll build
A simple user API with these endpoints:
GET /users - List all users
GET /users/123 - Get a specific user
GET /health - Health check endpoint
Prerequisites
moclojer installed ()
A text editor
Command line access
curl or a tool to make HTTP requests
Step 1: Create your configuration file
Create a new file called moclojer.yml in an empty directory:
/users/1 endpoint returns details for a specific user
Each response includes different data structures (array vs object)
Step 5: Test your expanded API
Restart your server and test the new endpoints:
# Test the users list
curl http://localhost:8000/users
# Test a specific user
curl http://localhost:8000/users/1
# Test the health check still works
curl http://localhost:8000/health
Step 6: Handle different HTTP methods
Let's add a POST endpoint to create users. Add this to your moclojer.yml:
Real APIs return errors sometimes. Let's add a 404 response:
- endpoint:
method: GET
path: /users/999
response:
status: 404
headers:
Content-Type: application/json
body: >
{
"error": "User not found",
"code": "USER_NOT_FOUND",
"message": "User with ID 999 does not exist"
}
Test it:
curl http://localhost:8000/users/999
Understanding the configuration
Let's break down what you've learned:
Endpoint structure
- endpoint: # Start of endpoint definition
method: GET # HTTP method (GET, POST, PUT, DELETE, etc.)
path: /users # URL path
response: # What to return
status: 200 # HTTP status code
headers: # HTTP headers
Content-Type: application/json
body: > # Response content
{"message": "Hello"}
Key concepts
Method: What HTTP verb this endpoint responds to
Path: The URL pattern to match
Status: HTTP status code (200=success, 404=not found, etc.)
✅ Created a working mock API server
✅ Defined multiple endpoints with different HTTP methods
✅ Structured JSON responses
✅ Added error handling
✅ Tested everything with curl
You now have a solid foundation! But your API is still static - it always returns the same responses. In the next tutorial, you'll learn how to make your responses dynamic based on the request data.
Need help?
👉 - Learn to use path parameters, query parameters, and templates