extractStrictEntity

Signature

def extractStrictEntity(timeout: FiniteDuration): Directive1[HttpEntity.Strict]
def extractStrictEntity(timeout: FiniteDuration, maxBytes: Long): Directive1[HttpEntity.Strict]

Description

Extracts the strict http entity as HttpEntity.Strict from the RequestContextRequestContext.

A timeout parameter is given and if the stream isn’t completed after the timeout, the directive will be failed.

Warning

The directive will read the request entity into memory within the size limit(8M by default) and effectively disable streaming. The size limit can be configured globally with akka.http.parsing.max-content-length or overridden by wrapping with withSizeLimit or withoutSizeLimit directive.

Example

Scala
sourceimport scala.concurrent.duration._
val route = extractStrictEntity(3.seconds) { entity =>
  complete(entity.data.utf8String)
}

// tests:
val dataBytes = Source.fromIterator(() => Iterator.range(1, 10).map(x => ByteString(x.toString)))
Post("/", HttpEntity(ContentTypes.`text/plain(UTF-8)`, data = dataBytes)) ~> route ~> check {
  responseAs[String] shouldEqual "123456789"
}
Java
sourceimport static akka.http.javadsl.server.Directives.extractStrictEntity;

final FiniteDuration timeout = FiniteDuration.create(3, TimeUnit.SECONDS);
final Route route = extractStrictEntity(timeout, strict ->
  complete(strict.getData().utf8String())
);

// tests:
final Iterator<ByteString> iterator = Arrays.asList(
  ByteString.fromString("1"),
  ByteString.fromString("2"),
  ByteString.fromString("3")).iterator();
final Source<ByteString, NotUsed> dataBytes = Source.fromIterator(() -> iterator);
testRoute(route).run(
  HttpRequest.POST("/")
    .withEntity(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, dataBytes))
).assertEntity("123");
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.