'How to get full list of request headers in Micronaut
I am trying to get a list of all headers sent in a http request in a Micronaut @Controller.
Solution 1:[1]
You need to add a variable of type io.micronaut.http.HttpHeaders
to your API endpoint parameters. Give this a shot:
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.HttpResponse;
@Controller
public class TestController {
@Get("/test/http-headers")
public HttpResponse<String> getHttpHeaders(HttpHeaders headers) {
for (val header : headers.entrySet()) {
// Do stuff with header.getKey() and header.getValue()
}
return HttpResponse.ok("Testing");
}
}
Latest docs on HttpHeaders
: https://docs.micronaut.io/latest/api/io/micronaut/http/HttpHeaders.html#method.summary
Solution 2:[2]
https://docs.micronaut.io/latest/guide/index.html#requestResponse
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
@Controller("/request")
public class MessageController {
@Get("/hello")
public HttpResponse<String> hello(HttpHeaders headers) {
// deal with the headers
return HttpResponse.ok("Hello world!!")
.header("X-My-Header", "Foo");
}
}
https://docs.micronaut.io/latest/api/io/micronaut/http/HttpHeaders.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mass Dot Net |
Solution 2 | IEE1394 |