If you are looking for reading and accessing request headers from your spring boot kotlin application, here are some example implementation that you can follow.

Reading Header in Kotlin From Spring Http Request

To read headers you can use @RequestHeader annotation to read one specific headers or all headers. Let's see some different implementation that we can use using this annotation.

Reading a single header

In the example I tried to read only "User-Agent" from the header

@RequestHeader("User-Agent") userAgent: String

Output for the example implementation given in the snippet

$ curl localhost:8080/single-header

Output: UserAgent: curl/7.64.1

Reading all headers

You can read all headers in three ways

@RequestHeader headers: Map<String, String>
@RequestHeader headers: MultiValueMap<String, String>
@RequestHeader headers: HttpHeaders

Here are some outputs for the given implementations

Console output for: /all-headers

Content-Type: application/json
User-Agent: PostmanRuntime/7.26.1
Accept: */*
Cache-Control: no-cache
Postman-Token: ea77d4ec-cb1a-40a1-8c88-8799dbcd2e44
Host: localhost:8081
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 773

Console output for: /header-multivalued

Content-Type: application/json
User-Agent: PostmanRuntime/7.26.1
Accept: */*
Cache-Control: no-cache
Postman-Token: ea77d4ec-cb1a-40a1-8c88-8799dbcd2e44
Host: localhost:8081
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 773
Content-Type: [application/json]
User-Agent: [PostmanRuntime/7.26.1]
Accept: [*/*]
Cache-Control: [no-cache]
Postman-Token: [6ba97619-3665-400e-aaf0-dcc72c683080]
Host: [localhost:8081]
Accept-Encoding: [gzip, deflate, br]
Connection: [keep-alive]
Content-Length: [773]