extractRequestEntity

Signature

def extractRequestEntity: Directive1[RequestEntity]

Description

Extracts the RequestEntityRequestEntity from the RequestContextRequestContext.

The directive returns a RequestEntityRequestEntity without unmarshalling the request. To extract domain entity, entity should be used.

Example

Scala
val route =
  extractRequestEntity { entity =>
    complete(s"Request entity content-type is ${entity.contentType}")
  }

// tests:
val httpEntity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, "req")
Post("/abc", httpEntity) ~> route ~> check {
  responseAs[String] shouldEqual "Request entity content-type is text/plain; charset=UTF-8"
}
Java
import static akka.http.javadsl.server.Directives.extractRequestEntity;

final Route route = extractRequestEntity(entity ->
  complete("Request entity content-type is " + entity.getContentType())
);

// tests:
testRoute(route).run(
  HttpRequest.POST("/abc")
    .withEntity(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, "req"))
).assertEntity("Request entity content-type is text/plain; charset=UTF-8");
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.