headerValueByName

headerValueByName

§Description

Extracts the value of the HTTP request header with the given name.

If no header with a matching name is found the request is rejected with a MissingHeaderRejection.

If the header is expected to be missing in some cases or to customize handling when the header is missing use the optionalHeaderValueByName directive instead.

§Example

  1. final Route route = headerValueByName("X-User-Id", userId ->
  2. complete("The user is " + userId)
  3. );
  4.  
  5. // tests:
  6. final RawHeader header = RawHeader.create("X-User-Id", "Joe42");
  7. testRoute(route).run(HttpRequest.GET("/").addHeader(header))
  8. .assertEntity("The user is Joe42");
  9.  
  10. testRoute(route).run(HttpRequest.GET("/"))
  11. .assertStatusCode(StatusCodes.BAD_REQUEST)
  12. .assertEntity("Request is missing required HTTP header 'X-User-Id'");