Can you explain how Akka HTTP’s Media Types and Content Negotiation features work? Can you provide an example of a use case for each?

Akka HTTP’s Media Types define the format of data exchanged between client and server, while Content Negotiation allows clients to request specific media types. The server selects the best matching response based on available formats.

Media Types are represented as objects with a main type (e.g., “application”) and a subtype (e.g., “json”). They can have parameters like charset for text-based types. Akka HTTP provides predefined media types and supports custom ones.

Content Negotiation involves two headers : Accept from the client, indicating desired media types, and Content-Type from the server, specifying the chosen format. Clients may provide multiple acceptable types with quality factors (q-values) to indicate preference.

Use case – Media Types : An API returns JSON or XML depending on the endpoint. Define custom media types for each:
val jsonType = MediaType.applicationWithFixedCharset("custom-json", HttpCharsets.`UTF-8`)
val xmlType = MediaType.applicationWithFixedCharset("custom-xml", HttpCharsets.`UTF-8`)?


Use case – Content Negotiation : A client requests an image in JPEG or PNG format, preferring JPEG. Server responds with the appropriate format:
Client header : Accept: image/jpeg;q=0.9, image/png;q=0.8

Server code :

import akka.http.scaladsl.model._
import MediaTypes._
val imageData: Array[Byte] = ...
val contentType = if (request.header[headers.Accept].exists(_.mediaRanges.exists(_.matches(`image/jpeg`)))) {
  ContentType(`image/jpeg`)
} else {
  ContentType(`image/png`)
}
HttpResponse(entity = HttpEntity(contentType, imageData))?