requestEntityPresent

Signature

def requestEntityPresent: Directive0

Description

A simple filter that checks if the request entity is present and only then passes processing to the inner route. Otherwise, the request is rejected with RequestEntityExpectedRejection.

See also requestEntityEmpty for the opposite effect.

Example

Scala
sourceval route =
  concat(
    requestEntityEmpty {
      complete("request entity empty")
    },
    requestEntityPresent {
      complete("request entity present")
    }
  )

// tests:
Post("/", "text") ~> Route.seal(route) ~> check {
  responseAs[String] shouldEqual "request entity present"
}
Post("/") ~> route ~> check {
  responseAs[String] shouldEqual "request entity empty"
}
Java
sourceimport static akka.http.javadsl.server.Directives.complete;
import static akka.http.javadsl.server.Directives.requestEntityEmpty;
import static akka.http.javadsl.server.Directives.requestEntityPresent;

final Route route = requestEntityEmpty(() ->
  complete("request entity empty")
).orElse(requestEntityPresent(() ->
  complete("request entity present")
));

// tests:
testRoute(route).run(HttpRequest.POST("/"))
  .assertEntity("request entity empty");
testRoute(route).run(HttpRequest.POST("/").withEntity("foo"))
  .assertEntity("request entity present");
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.