diff --git a/docs/src/modules/java/pages/http-endpoints.adoc b/docs/src/modules/java/pages/http-endpoints.adoc index 7a3a3ba0f..6ccf95de2 100644 --- a/docs/src/modules/java/pages/http-endpoints.adoc +++ b/docs/src/modules/java/pages/http-endpoints.adoc @@ -249,4 +249,11 @@ include::example$doc-snippets/src/main/java/com/example/api/ExampleEndpoint.java Accessing request headers is done through the link:_attachments/api/akka/javasdk/http/RequestContext.html[RequestContext] methods `requestHeader(String headerName)` and `allRequestHeaders()`. -ifdef::todo[TODO: add full snippet once rebased on endpoint baaseclass PR] \ No newline at end of file +By letting the endpoint extend link:_attachments/api/akka/javasdk/http/AbstractHttpEndpoint.html[AbstractHttpEndpoint] request context is available through the method `requestContext()`. + +[source,java] +.{sample-base-url}/doc-snippets/src/main/java/com/example/api/ExampleEndpoint.java[ExampleEndpoint.java] +---- +include::example$doc-snippets/src/main/java/com/example/api/ExampleEndpoint.java[tag=header-access] +---- +<1> `requestHeader(headerName)` returns an `Optional` which is empty if the header was not present. diff --git a/samples/doc-snippets/src/main/java/com/example/api/ExampleEndpoint.java b/samples/doc-snippets/src/main/java/com/example/api/ExampleEndpoint.java index 32eff24f3..e8baf8d6d 100644 --- a/samples/doc-snippets/src/main/java/com/example/api/ExampleEndpoint.java +++ b/samples/doc-snippets/src/main/java/com/example/api/ExampleEndpoint.java @@ -153,4 +153,14 @@ public CompletionStage lowerLevelRequestHello(String name, HttpRequest r } } // end::lower-level-request[] + + // tag::header-access[] + @Get("/header-access") + public String headerAccessExample() { + var userAgentHeader = requestContext().requestHeader("User-Agent"); // <1> + + return userAgentHeader.map(httpHeader -> "User agent: " + httpHeader.value()) + .orElse("No user agent header present"); + } + // end::header-access[] }