Content Negotiation

Content negotiation in Azu allows your API to serve different content types based on client preferences. With support for multiple formats, automatic content type detection, and flexible response handling, content negotiation makes your API more versatile and user-friendly.

What is Content Negotiation?

Content negotiation in Azu provides:

  • Multiple Formats: Support for JSON, XML, HTML, and custom formats

  • Automatic Detection: Detect client preferences from headers

  • Flexible Responses: Serve different content types for the same endpoint

  • Format Validation: Validate content types and handle errors

  • Custom Serializers: Implement custom serialization logic

Basic Content Negotiation

Accept Header Detection

class ContentNegotiationEndpoint
  include Azu::Endpoint(Azu::Request::Empty, Azu::Response::Text)

  get "/api/data"

  def call : Azu::Response::Text
    # Detect client preferences
    accept_header = context.request.headers["Accept"]?
    content_type = determine_content_type(accept_header)

    # Set response content type
    context.response.headers["Content-Type"] = content_type

    # Generate response based on content type
    case content_type
    when "application/json"
      generate_json_response
    when "application/xml"
      generate_xml_response
    when "text/html"
      generate_html_response
    else
      generate_json_response  # Default to JSON
    end
  end

  private def determine_content_type(accept_header : String?) : String
    return "application/json" unless accept_header

    # Parse Accept header
    preferences = parse_accept_header(accept_header)

    # Find best match
    if preferences.includes?("application/json")
      "application/json"
    elsif preferences.includes?("application/xml")
      "application/xml"
    elsif preferences.includes?("text/html")
      "text/html"
    else
      "application/json"  # Default
    end
  end

  private def parse_accept_header(accept_header : String) : Array(String)
    accept_header.split(",").map(&.strip)
  end
end

Content Type Validation

Multiple Format Support

JSON Response

XML Response

HTML Response

Content Negotiation Middleware

Content Negotiation Middleware

Format Handlers

JSON Handler

XML Handler

HTML Handler

Custom Format Support

Custom Format Handler

Format Registry

Content Negotiation in Endpoints

Flexible Endpoint

Multi-Format Endpoint

Error Handling

Content Negotiation Errors

Testing Content Negotiation

Unit Testing

Integration Testing

Best Practices

1. Use Appropriate Content Types

2. Handle Content Negotiation Errors

3. Validate Content Types

4. Use Quality Values

5. Provide Fallbacks

Next Steps

Now that you understand content negotiation:

  1. API Design - Design flexible APIs

  2. Testing - Test content negotiation

  3. Performance - Optimize content negotiation

  4. Deployment - Deploy with content negotiation

  5. Security - Implement secure content negotiation


Content negotiation in Azu provides flexible, client-aware API responses. With support for multiple formats, automatic detection, and error handling, it makes your API more versatile and user-friendly.

Last updated

Was this helpful?