Skip to main content

Content Negotiation & Response Formatting

The content negotiation utilities provide intelligent content type detection, response formatting, and media type handling for edge environments. These utilities help deliver the right content format to clients while optimizing for performance and compatibility.

Features

  • Content Type Detection: Automatic content type negotiation
  • Format Conversion: Convert between JSON, XML, CSV, and other formats
  • Media Type Handling: Support for custom media types and quality values
  • Performance Optimized: Efficient parsing and serialization
  • Internationalization: Character encoding and language negotiation
  • Compression: Automatic content compression
  • Content Validation: Schema validation and sanitization
  • Client Preferences: Respect Accept headers and client capabilities

Quick Start

Content Negotiation

Accept Header Parsing

Parse and interpret HTTP Accept headers for content type negotiation.

Media Type Matching

Advanced media type matching with wildcards and parameters.

Custom Negotiation Logic

Implement custom content negotiation strategies.

Response Formatting

JSON Formatting

Format data as JSON with customization options.

XML Formatting

Convert data structures to XML format.

CSV Formatting

Generate CSV responses from tabular data.

Custom Formatters

Create custom response formatters for specialized content types.

Content Compression

Automatic Compression

Automatically compress responses based on client capabilities.

Manual Compression

Control compression manually for specific use cases.

Character Encoding

Encoding Detection and Conversion

Handle different character encodings automatically.

Internationalization Support

Handle internationalization and localization in responses.

Content Validation

Schema Validation

Validate response data against schemas before formatting.

Content Sanitization

Sanitize content to prevent XSS and other security issues.

Middleware Integration

Content Negotiation Middleware

API Versioning Middleware

Advanced Examples

REST API with Content Negotiation

GraphQL API with Content Negotiation

Streaming Responses

Performance Considerations

Response Caching

Cache formatted responses to improve performance.

Lazy Formatting

Format responses only when needed.

Memory Management

Handle large responses efficiently.

Platform-Specific Notes

Cloudflare Workers

  • Compatible with Cloudflare’s streaming responses
  • Use Cloudflare KV for caching formatted responses
  • Support for Cloudflare’s compression

Vercel Edge Functions

  • Native streaming support
  • Compatible with Vercel’s edge network
  • Support for Vercel’s deployment regions

Deno Deploy

  • Native support for content negotiation
  • Compatible with Deno’s Web APIs
  • Support for streaming responses

Best Practices

Content Type Selection

Choose appropriate content types for your API:

Error Response Formatting

Format errors consistently across content types:

Content Negotiation Headers

Include helpful headers in responses:

Testing

Run content negotiation tests with:

API Reference

ContentNegotiator Methods

  • negotiate(request) - Negotiate content type for request
  • parseAccept(acceptHeader) - Parse Accept header
  • supports(type) - Check if type is supported
  • getSupportedTypes() - Get all supported types

ResponseFormatter Methods

  • format(data, options) - Format data into response
  • formatJSON(data, options) - Format as JSON
  • formatXML(data, options) - Format as XML
  • formatCSV(data, options) - Format as CSV

ContentCompressor Methods

  • compress(data, algorithm) - Compress data
  • decompress(data, algorithm) - Decompress data
  • getSupportedAlgorithms() - Get supported algorithms

Contributing

When contributing to content negotiation utilities:
  1. Maintain backward compatibility
  2. Add comprehensive tests for new formats
  3. Update documentation for new features
  4. Consider performance impact of changes
  5. Test across all supported platforms

License

MIT