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 UnsupportedRequestEncodingRejection
UnsupportedRequestEncodingRejection
. If no decoders are given the default encoders (Gzip
, Deflate
, NoCoding
) are used. If the request entity after decoding exceeds pekko.http.routing.decode-max-size
the stream fails with an EntityStreamSizeException
EntityStreamSizeException
.
Example
- Scala
-
source
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
-
source
import static org.apache.pekko.http.javadsl.server.Directives.complete; import static org.apache.pekko.http.javadsl.server.Directives.decodeRequestWith; import static org.apache.pekko.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));
1.1.0+17-3b5f9b27*