This directory contains example files for the Go JSON Server. These examples demonstrate various features of the server including:
- Basic API endpoints
- Path parameters (
:id
,:userId
,:postId
) - Static file serving
- Different HTTP methods (GET, POST)
- Various response status codes
api.json
- The main configuration file for the serverhealth-check.json
- Simple health check endpoint responseusers.json
- List of usersuser-detail.json
- Detailed user information with path parameter supportposts.json
- List of blog postspost-detail.json
- Detailed post information with path parameter supportuser-post.json
- Demonstrates multiple path parameters in one endpointuser-created.json
- Example response for a POST requeststatic/
- Directory for static filessample.jpg
- Example image fileindex.html
- Example HTML documentation page
From the root directory of the project:
go run go-json-server.go --config=./example/api.json
Or if you've installed the binary:
go-json-server --config=./example/api.json
You can use curl, Postman, or any HTTP client to test the endpoints:
# Get health check
curl http://localhost:3000/
# Get all users
curl http://localhost:3000/users
# Get user with ID 1
curl http://localhost:3000/user/1
# Get all posts
curl http://localhost:3000/posts
# Get post with ID 2
curl http://localhost:3000/posts/2
# Get post 3 by user 2
curl http://localhost:3000/users/2/posts/3
# Create a new user
curl -X POST http://localhost:3000/users
# Access static HTML page
curl http://localhost:3000/static/index.html
# Or open in browser: http://localhost:3000/static/index.html
The paths in the api.json
configuration file are relative to the directory from which you run the server, not the location of the configuration file itself. This means that when running from the project root, all paths include the example/
prefix, like:
{
"jsonPath": "./example/health-check.json"
}
If you want to run the server while in the example directory, you would need to modify the paths in api.json
to remove the example/
prefix.
Feel free to modify these example files or create your own to experiment with the Go JSON Server's capabilities.