Skip to main content
Kit provides a flexible response system inspired by Laravel, allowing you to create JSON responses, text responses, redirects, and handle errors elegantly. The Response type enables using Rust’s ? operator for clean error propagation.

The Response Type

In Kit, controller handlers return a Response type, which is an alias for Result<HttpResponse, HttpResponse>:
This design allows you to:
  • Return successful responses with Ok(HttpResponse::...)
  • Return error responses with Err(HttpResponse::...)
  • Use the ? operator for automatic error conversion

Creating Responses

JSON Responses

The most common response type. Use HttpResponse::json() or the json_response! macro:
Or with the convenient json_response! macro:

Text Responses

For plain text responses:
Or with the text_response! macro:

Setting Status Codes

Chain .status() to set the HTTP status code:

Adding Headers

Add custom headers with .header():

Redirects

Kit provides two ways to create redirects:

Simple Redirects

Redirect to a specific URL or path:

Named Route Redirects

Redirect to a named route using the redirect! macro (with compile-time route validation):

Redirects with Parameters

Add route parameters and query strings:

Permanent Redirects

Use .permanent() for 301 redirects (default is 302):

Error Handling

Kit automatically converts errors to appropriate HTTP responses when using the ? operator.

Using the ? Operator

Errors are automatically converted to JSON error responses:

AppError for Custom Errors

Use AppError for domain-specific errors with custom status codes:

AppError Helper Methods

Custom Status Codes

Set any status code with .status():

FrameworkError Types

Kit’s FrameworkError handles common error scenarios:

Error Response Format

Errors are returned as JSON with appropriate structure:

Implementing HttpError Trait

For custom error types, implement the HttpError trait:

Response Macros

Kit provides convenient macros for creating responses:

Complete Example

Summary