Skip to content

RestApi

The default policy. Generates conventional REST endpoints — one URL per operation, JSON request/response bodies wrapped in a model key.

  • MIME: application/json
  • Serializer: RestApiSerializer
  • Routing: Path-based (/models, /models/:id)
import crate.protocols.restApi.policy;
// These are equivalent:
auto crateRouter = router.crateSetup;
auto crateRouter = router.crateSetup!RestApi;
crateRouter.add(userCrate);
MethodPathOperationStatus
GET/usersList all200
GET/users/:idGet one200
POST/usersCreate200
PUT/users/:idReplace200
PATCH/users/:idUpdate fields200
DELETE/users/:idDelete204

Requests wrap data in a model key:

{
"user": {
"name": "Alice",
"email": "alice@example.com"
}
}

Responses use the same wrapping:

{
"user": {
"_id": "abc123",
"name": "Alice",
"email": "alice@example.com"
}
}

List responses use the plural key:

{
"users": [
{ "_id": "abc123", "name": "Alice" },
{ "_id": "def456", "name": "Bob" }
]
}

GET requests for single items include an ETag header (from the item’s hash field or CRC32 of the JSON). Clients can send If-None-Match to receive a 304 Not Modified when the item hasn’t changed.

The REST policy does not parse query parameters for pagination or sorting out of the box, but these features are available through middleware. The IQuery interface supports sort, limit, skip, and where — middleware can parse query parameters and apply them to the query.

  • Sparse fieldsets: No field selection via query params
  • HATEOAS links: No self/relationship links in responses
  • Bulk operations: No batch create/update/delete
auto crateRouter = router.crateSetup!(RestApiPolicy!(PolicyConfig("/api/v1")));
// Endpoints at /api/v1/users, /api/v1/users/:id, etc.