decodeRequestWith

Signature

def decodeRequestWith(decoder: Decoder): Directive0
def decodeRequestWith(decoders: Decoder*): Directive0

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 UnsupportedRequestEncodingRejectionUnsupportedRequestEncodingRejection. If no decoders are given the default encoders (Gzip, Deflate, NoCoding) are used. If the request entity after decoding exceeds akka.http.routing.decode-max-size the stream fails with an EntityStreamSizeExceptionEntityStreamSizeException.

Example

Scala
val route =
  decodeRequestWith(Coders.Gzip) {
    entity(as[String]) { content: String =>
      complete(s"Request content: '$content'")
    }
  }

// tests:
Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> route ~> check {
  responseAs[String] shouldEqual "Request content: 'Hello'"
}
Post("/", helloDeflated) ~> `Content-Encoding`(deflate) ~> route ~> check {
  rejection shouldEqual UnsupportedRequestEncodingRejection(gzip)
}
Post("/", "hello") ~> `Content-Encoding`(identity) ~> route ~> check {
  rejection shouldEqual UnsupportedRequestEncodingRejection(gzip)
}
val route =
  decodeRequestWith(Coders.Gzip, Coders.NoCoding) {
    entity(as[String]) { content: String =>
      complete(s"Request content: '$content'")
    }
  }

// tests:
Post("/", helloGzipped) ~> `Content-Encoding`(gzip) ~> route ~> check {
  responseAs[String] shouldEqual "Request content: 'Hello'"
}
Post("/", helloDeflated) ~> `Content-Encoding`(deflate) ~> route ~> check {
  rejections shouldEqual List(UnsupportedRequestEncodingRejection(gzip), UnsupportedRequestEncodingRejection(identity))
}
Post("/", "hello uncompressed") ~> `Content-Encoding`(identity) ~> route ~> check {
  responseAs[String] shouldEqual "Request content: 'hello uncompressed'"
}
Java
import static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.decodeRequestWith;
import static akka.http.javadsl.server.Directives.entity;

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));
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.