Skip to content

Commit

Permalink
docs and sample snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren committed Nov 21, 2024
1 parent 7c0d902 commit 4b602ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion docs/src/modules/java/pages/http-endpoints.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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.
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,14 @@ public CompletionStage<String> 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[]
}

0 comments on commit 4b602ce

Please sign in to comment.