6.8 The HttpRequest and HttpResponse

If you need more control over request processing then you can instead write a method that receives the complete HttpRequest.

In fact, there are several higher level interfaces that can be bound to method parameters of controllers. These include:

Table 1. Bindable Micronaut Interfaces
InterfaceDescriptionExample

HttpRequest

The full HttpRequest

String hello(HttpRequest request)

HttpHeaders

All HTTP headers present in the request

String hello(HttpHeaders headers)

HttpParameters

All HTTP parameters (either from URI variables or request parameters) present in the request

String hello(HttpParameters params)

Cookies

All the Cookies present in the request

String hello(Cookies cookies)

The HttpRequest should be declared parametrized with a concrete generic type if the request body is needed, e.g. HttpRequest<MyClass> request. The body may not be available from the request otherwise.

In addition, for full control over the emitted HTTP response you can use the static factory methods of the HttpResponse class which return a MutableHttpResponse.

The following example implements the previous MessageController example using the HttpRequest and HttpResponse objects:

Request and Response Example

  1. import io.micronaut.http.HttpRequest;
  2. import io.micronaut.http.HttpResponse;
  3. import io.micronaut.http.annotation.Controller;
  4. import io.micronaut.http.annotation.Get;
  5. @Controller("/request")
  6. public class MessageController {
  7. @Get("/hello") (1)
  8. public HttpResponse<String> hello(HttpRequest<?> request) {
  9. String name = request.getParameters()
  10. .getFirst("name")
  11. .orElse("Nobody"); (2)
  12. return HttpResponse.ok("Hello " + name + "!!")
  13. .header("X-My-Header", "Foo"); (3)
  14. }
  15. }

Request and Response Example

  1. import io.micronaut.http.HttpRequest
  2. import io.micronaut.http.HttpResponse
  3. import io.micronaut.http.annotation.Controller
  4. import io.micronaut.http.annotation.Get
  5. @Controller("/request")
  6. class MessageController {
  7. @Get("/hello") (1)
  8. HttpResponse<String> hello(HttpRequest<?> request) {
  9. String name = request.getParameters()
  10. .getFirst("name")
  11. .orElse("Nobody") (2)
  12. HttpResponse.ok("Hello " + name + "!!")
  13. .header("X-My-Header", "Foo") (3)
  14. }
  15. }

Request and Response Example

  1. import io.micronaut.http.HttpRequest
  2. import io.micronaut.http.HttpResponse
  3. import io.micronaut.http.annotation.Controller
  4. import io.micronaut.http.annotation.Get
  5. @Controller("/request")
  6. class MessageController {
  7. @Get("/hello") (1)
  8. fun hello(request: HttpRequest<*>): HttpResponse<String> {
  9. val name = request.parameters
  10. .getFirst("name")
  11. .orElse("Nobody") (2)
  12. return HttpResponse.ok("Hello $name!!")
  13. .header("X-My-Header", "Foo") (3)
  14. }
  15. }
1The method is mapped to the URI /hello and accepts a HttpRequest
2The HttpRequest is used to obtain the value of a query parameter called name.
3The HttpResponse.ok(T) method is used to return a MutableHttpResponse with a text body. A header called X-My-Header is also added to the response object.

HttpRequest is also available from static context via ServerRequestContext.

Generally ServerRequestContext is available within reactive flow, but the recommended approach is to propagate the necessary state through lambdas.