Response

Response objects define how endpoint results are rendered to clients.

Including Response

struct MyResponse
  include Azu::Response

  def initialize(@data : MyData)
  end

  def render
    @data.to_json
  end
end

Required Methods

render

Return the response body as a string.

def render : String
  {id: @user.id, name: @user.name}.to_json
end

Returns: String

Built-in Response Types

Azu::Response::Json

JSON response with automatic content type.

Azu::Response::Text

Plain text response.

Azu::Response::Html

HTML response.

Azu::Response::Empty

No content response (204).

Error Responses

Azu::Response::Error

Base class for error responses.

Built-in Errors

Class
Status
Usage

BadRequest

400

Invalid request

Unauthorized

401

Authentication required

Forbidden

403

Access denied

NotFound

404

Resource not found

MethodNotAllowed

405

HTTP method not allowed

Conflict

409

Resource conflict

UnprocessableEntity

422

Validation failed

TooManyRequests

429

Rate limit exceeded

InternalServerError

500

Server error

ServiceUnavailable

503

Service unavailable

Using Error Responses

ValidationError

Special error for validation failures.

Custom Response Example

Response with Headers

Set custom headers in your endpoint:

Response with Status

Set custom status codes:

Streaming Responses

For large responses:

Content Negotiation

Return different formats based on Accept header:

See Also

Last updated

Was this helpful?