decodeRequestWith

Description

Decodes the incoming request if it is encoded with one of the given encoders. If the request encoding doesn’t match one of the given encoders the request is rejected with an UnsupportedRequestEncodingRejection. If no decoders are given the default encoders (Gzip, Deflate, NoCoding) are used.

Example

final ByteString helloGzipped = Coder.Gzip.encode(ByteString.fromString("Hello"));
final ByteString helloDeflated = Coder.Deflate.encode(ByteString.fromString("Hello"));

final Route route = decodeRequestWith(Coder.Gzip, () ->
  entity(entityToString(), content ->
    complete("Request content: '" + content + "'")
  )
);

// tests:
testRoute(route).run(
  HttpRequest.POST("/").withEntity(helloGzipped)
    .addHeader(ContentEncoding.create(HttpEncodings.GZIP)))
  .assertEntity("Request content: 'Hello'");

runRouteUnSealed(route,
  HttpRequest.POST("/").withEntity(helloDeflated)
    .addHeader(ContentEncoding.create(HttpEncodings.DEFLATE)))
  .assertRejections(Rejections.unsupportedRequestEncoding(HttpEncodings.GZIP));

runRouteUnSealed(route,
  HttpRequest.POST("/").withEntity("hello")
    .addHeader(ContentEncoding.create(HttpEncodings.IDENTITY)))
  .assertRejections(Rejections.unsupportedRequestEncoding(HttpEncodings.GZIP));
The source code for this page can be found here.