Class HttpRequest


  • public class HttpRequest
    extends Object

    Provides an immutable HTTP request class.

    The example bellow illustrates using the request object for debugging.

    
     import io.nitric.http.HttpHandler;
     import io.nitric.http.HttpRequest;
     import io.nitric.http.HttpResponse;
     import io.nitric.http.HttpServer;
    
     public class RequestInfo {
    
         public static void main(String... args) {
    
             new HttpServer().start((HttpRequest r) -gt; {
                 var logger = System.out;
    
                 logger.printf("request: \n");
                 logger.printf("   .method: %s \n", r.getMethod());
                 logger.printf("   .path: %s \n", r.getPath());
    
                 logger.printf("   .headers: \n");
                 for (String name : r.getHeaders().keySet()) {
                     logger.printf("      %s: %s \n", name, r.getHeaders().get(name));
                 }
    
                 logger.printf("   .parameters: \n");
                 for (String name : r.getParameters().keySet()) {
                     logger.printf("      %s: %s \n", name, r.getParameters().get(name));
    
                 }
    
                 logger.printf("   .body: \n");
                 logger.printf("      %s \n", r.getBodyText());
    
                 return HttpResponse.build(200);
             });
         }
     }
     
    Since:
    1.0
    See Also:
    HttpHandler, HttpResponse, HttpResponse.Builder
    • Method Detail

      • getMethod

        public String getMethod()
        Returns:
        the HTTP operation method [ GET, POST, PUT, DELETE ].
      • getPath

        public String getPath()
        Returns:
        the request path or null if not defined.
      • getQuery

        public String getQuery()
        Returns:
        the GET request query string value.
      • getHeaders

        public Map<String,​List<String>> getHeaders()
        Returns:
        an immutable map of HTTP request headers, an empty map will be returned if there are no headers.
      • getHeader

        public String getHeader​(String name)
        Return the named HTTP header or null if not found. If the header has multiple values the first value will be returned. Please note Nitric headers are case-insensitive
        Parameters:
        name - the name of the HTTP header
        Returns:
        the named HTTP header or null if not found
      • getBody

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

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

        public String getContentType()
        Return the 'Content-Type' header value, or the MIME type of the request body. If the 'Content-Type' header is not defined this method will return null.
        Returns:
        the 'Content-Type' header value, or null if the not defined.
      • getParameters

        public Map<String,​List<String>> getParameters()

        Return map of request parameter values, parsed from GET URL query or POST form encoded values Note an empty empty map will be returned if there are no headers.

        Note this method does currently support parsing parameters with form 'multipart/form-data' post requests.

        Returns:
        map of request parameter values, or an empty map one are defined.
      • getParameter

        public String getParameter​(String name)
        Return the first parameter value defined for the given name, or null if not found. Note this method does currently support form 'multipart/form-data' post requests.
        Parameters:
        name - the parameter name
        Returns:
        first parameter value defined for the given name, or null if not found
      • toString

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

        public static HttpRequest.Builder newBuilder()
        Returns:
        a new HttpRequest builder class.