This documentation regards version 10.1, however the current version is 10.7.0 .
encodeResponseWith
Signature
def encodeResponseWith ( first : Encoder , more : Encoder *): Directive0
Description
Encodes the response with the encoding that is requested by the client via the Accept-Encoding
if it is among the provided encoders or rejects the request with an UnacceptedResponseEncodingRejection(supportedEncodings)
.
The response encoding is determined by the rules specified in RFC7231 .
If the Accept-Encoding
header is missing then the response is encoded using the first
encoder.
If the Accept-Encoding
header is empty and NoCoding
is part of the encoders then no response encoding is used. Otherwise the request is rejected.
Example
Scala
copy source val route = encodeResponseWith ( Gzip ) { complete ( "content" ) }
// tests:
Get ( "/" ) ~> route ~> check {
response should haveContentEncoding ( gzip )
}
Get ( "/" ) ~> `Accept-Encoding` ( gzip , deflate ) ~> route ~> check {
response should haveContentEncoding ( gzip )
}
Get ( "/" ) ~> `Accept-Encoding` ( deflate ) ~> route ~> check {
rejection shouldEqual UnacceptedResponseEncodingRejection ( gzip )
}
Get ( "/" ) ~> `Accept-Encoding` ( identity ) ~> route ~> check {
rejection shouldEqual UnacceptedResponseEncodingRejection ( gzip )
}
// with custom compression level:
val routeWithLevel9 = encodeResponseWith ( Gzip . withLevel ( 9 )) { complete ( "content" ) }
Get ( "/" ) ~> routeWithLevel9 ~> check {
response should haveContentEncoding ( gzip )
}
Java
copy source import static akka . http . javadsl . server . Directives . complete ;
import static akka . http . javadsl . server . Directives . encodeResponseWith ;
final Route route = encodeResponseWith (
Collections . singletonList ( Coder . Gzip ),
() -> complete ( "content" )
);
// tests:
testRoute ( route ). run ( HttpRequest . GET ( "/" ))
. assertHeaderExists ( ContentEncoding . create ( HttpEncodings . GZIP ));
testRoute ( route ). run (
HttpRequest . GET ( "/" )
. addHeader ( AcceptEncoding . create ( HttpEncodings . GZIP ))
. addHeader ( AcceptEncoding . create ( HttpEncodings . DEFLATE ))
). assertHeaderExists ( ContentEncoding . create ( HttpEncodings . GZIP ));
runRouteUnSealed ( route ,
HttpRequest . GET ( "/" )
. addHeader ( AcceptEncoding . create ( HttpEncodings . DEFLATE ))
). assertRejections ( Rejections . unacceptedResponseEncoding ( HttpEncodings . GZIP ));
runRouteUnSealed ( route ,
HttpRequest . GET ( "/" )
. addHeader ( AcceptEncoding . create ( HttpEncodings . IDENTITY ))
). assertRejections ( Rejections . unacceptedResponseEncoding ( HttpEncodings . GZIP ));
final Route routeWithLevel9 = encodeResponseWith (
Collections . singletonList ( Coder . GzipLevel9 ),
() -> complete ( "content" )
);
testRoute ( routeWithLevel9 ). run ( HttpRequest . GET ( "/" ))
. assertHeaderExists ( ContentEncoding . create ( 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.