Class HttpResponse


  • public class HttpResponse
    extends Object

    Provides an immutable HTTP response class. This class provides static convenience methods for quickly creating a response and a HttpResponse.Builder class for fine grained control.

    The HTTP examples below use the static HttpResponse build methods:

    
     import io.nitric.http.HttpResponse;
     ...
    
     // 404 - Not Found response
     return HttpResponse.build(404);
    
     // 200 - JSON message
     var json = "{ \"status\": \"online\" }";
     return HttpResponse.build(200, json);
    
     // 418 - Error message
     return HttpResponse.build(418, "Im a tea pot");
     

    The example below uses the HttpResponse.Builder class:

    
     import io.nitric.http.HttpResponse;
     ...
    
     byte[] data = Files.readAllBytes(path);
    
     return HttpResponse.newBuilder()
                .header("Content-Type", "application/jar")
                .body(data)
                .build();
     
    See Also:
    HttpRequest, HttpHandler
    • Method Detail

      • getStatus

        public int getStatus()
        Returns:
        the HTTP response status, e.g. 200 for HTTP OK
      • getHeaders

        public Map<String,​List<String>> getHeaders()
        Returns:
        an immutable map of function response headers
      • getHeader

        public String getHeader​(String name)
        Return the named response header or null if not found. If the header has multiple values the first value will be returned.
        Parameters:
        name - the name of the Nitric header
        Returns:
        the named function header or null if not found
      • getBody

        public byte[] getBody()
        Returns:
        the response body
      • getBodyText

        public String getBodyText()
        Returns:
        the response body as text (UTF-8 encoded)
      • getBodyLength

        public int getBodyLength()
        Returns:
        the function response body length, or -1 if no body present.
      • toString

        public String toString()
        Overrides:
        toString in class Object
        Returns:
        the string representation of this object
      • newBuilder

        public static HttpResponse.Builder newBuilder()
        Returns:
        a new HTTP response builder class.
      • build

        public static HttpResponse build​(String body)
        Return a new HTTP response object from the given body text.
        Parameters:
        body - the HTTP response body
        Returns:
        a new HttpResponse object
      • build

        public static HttpResponse build​(int status)
        Return a new HTTP response object from the given status.
        Parameters:
        status - the HTTP response status
        Returns:
        a new HttpResponse object
      • build

        public static HttpResponse build​(int status,
                                         String body)
        Return a new HttpResponse object from the given Nitric status and body text.
        Parameters:
        status - the response status
        body - the body text
        Returns:
        a new HttpResponse object