Package spark

Class Request

java.lang.Object
spark.Request

public class Request extends Object
Provides information about the HTTP request
Author:
Per Wendel
  • Constructor Details

    • Request

      protected Request()
  • Method Details

    • changeMatch

      protected void changeMatch(RouteMatch match)
    • params

      public Map<String,String> params()
      Returns the map containing all route params
      Returns:
      a map containing all route params
    • params

      public String params(String param)
      Returns the value of the provided route pattern parameter. Example: parameter 'name' from the following pattern: (get '/hello/:name')
      Parameters:
      param - the param
      Returns:
      null if the given param is null or not found
    • splat

      public String[] splat()
      Returns:
      an array containing the splat (wildcard) parameters
    • requestMethod

      public String requestMethod()
      Returns:
      request method e.g. GET, POST, PUT, ...
    • scheme

      public String scheme()
      Returns:
      the scheme
    • host

      public String host()
      Returns:
      the host
    • userAgent

      public String userAgent()
      Returns:
      the user-agent
    • port

      public int port()
      Returns:
      the server port
    • pathInfo

      public String pathInfo()
      Returns:
      the path info Example return: "/example/foo"
    • matchedPath

      public String matchedPath()
      Returns:
      the matched route Example return: "/account/:accountId"
    • servletPath

      public String servletPath()
      Returns:
      the servlet path
    • contextPath

      public String contextPath()
      Returns:
      the context path
    • url

      public String url()
      Returns:
      the URL string
    • contentType

      public String contentType()
      Returns:
      the content type of the body
    • ip

      public String ip()
      Returns:
      the client's IP address
    • body

      public String body()
      Returns:
      the request body sent by the client
    • bodyAsBytes

      public byte[] bodyAsBytes()
    • contentLength

      public int contentLength()
      Returns:
      the length of request.body
    • queryParams

      public String queryParams(String queryParam)
      Gets the query param
      Parameters:
      queryParam - the query parameter
      Returns:
      the value of the provided queryParam Example: query parameter 'id' from the following request URI: /hello?id=foo
    • queryParamsSafe

      public String queryParamsSafe(String queryParam)
      Gets the query param and encode it
      Parameters:
      queryParam - the query parameter
      Returns:
      the encode value of the provided queryParam Example: query parameter 'me' from the URI: /hello?id=fool.
    • queryParamOrDefault

      public String queryParamOrDefault(String queryParam, String defaultValue)
      Gets the query param, or returns default value
      Parameters:
      queryParam - the query parameter
      defaultValue - the default value
      Returns:
      the value of the provided queryParam, or default if value is null Example: query parameter 'id' from the following request URI: /hello?id=foo
    • queryParamsValues

      public String[] queryParamsValues(String queryParam)
      Gets all the values of the query param Example: query parameter 'id' from the following request URI: /hello?id=foo&id=bar
      Parameters:
      queryParam - the query parameter
      Returns:
      the values of the provided queryParam, null if it doesn't exists
    • headers

      public String headers(String header)
      Gets the value for the provided header
      Parameters:
      header - the header
      Returns:
      the value of the provided header
    • queryParams

      public Set<String> queryParams()
      Returns:
      all query parameters
    • headers

      public Set<String> headers()
      Returns:
      all headers
    • queryString

      public String queryString()
      Returns:
      the query string
    • attribute

      public void attribute(String attribute, Object value)
      Sets an attribute on the request (can be fetched in filters/routes later in the chain)
      Parameters:
      attribute - The attribute
      value - The attribute value
    • attribute

      public <T> T attribute(String attribute)
      Gets the value of the provided attribute
      Type Parameters:
      T - the type parameter.
      Parameters:
      attribute - The attribute value or null if not present
      Returns:
      the value for the provided attribute
    • attributes

      public Set<String> attributes()
      Returns:
      all attributes
    • raw

      public javax.servlet.http.HttpServletRequest raw()
      Returns:
      the raw HttpServletRequest object handed in by Jetty
    • queryMap

      public QueryParamsMap queryMap()
      Returns:
      the query map
    • queryMap

      public QueryParamsMap queryMap(String key)
      Parameters:
      key - the key
      Returns:
      the query map
    • session

      public Session session()
      Returns the current session associated with this request, or if the request does not have a session, creates one.
      Returns:
      the session associated with this request
    • session

      public Session session(boolean create)
      Returns the current session associated with this request, or if there is no current session and create is true, returns a new session.
      Parameters:
      create - true to create a new session for this request if necessary; false to return null if there's no current session
      Returns:
      the session associated with this request or null if create is false and the request has no valid session
    • cookies

      public Map<String,String> cookies()
      Returns:
      request cookies (or empty Map if cookies aren't present)
    • cookie

      public String cookie(String name)
      Gets cookie by name.
      Parameters:
      name - name of the cookie
      Returns:
      cookie value or null if the cookie was not found
    • uri

      public String uri()
      Returns:
      the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
    • protocol

      public String protocol()
      Returns:
      Returns the name and version of the protocol the request uses