Class Request

java.lang.Object
org.javalite.http.Request
Direct Known Subclasses:
Get, Multipart, Patch, Post

public abstract class Request extends Object
This class provides static convenience methods for simple HTTP requests.
Author:
Igor Polevoy
  • Field Details

    • redirect

      protected boolean redirect
    • url

      protected final String url
  • Constructor Details

    • Request

      public Request(String url, int connectTimeout, int readTimeout)
  • Method Details

    • header

      public <T extends Request> T header(String name, String value)
      Sets an HTTP header - call before making a request.
      Parameters:
      name - header name
      value - header value.
      Returns:
      self.
    • applicationJson

      public <T extends Request> T applicationJson()
      Convenience method to set a ""Content-Type" to "application/json" value.
      Returns:
      self
    • contentType

      public <T extends Request> T contentType(String value)
      Convenience method to set a ""Content-Type" header.
      Parameters:
      value - value for a "Content-Type" header.
      Returns:
      self
    • redirect

      public <T extends Request> T redirect(boolean redirect)
      Configures this request to follow redirects. Default is false.
      Parameters:
      redirect - true to follow, false to not.
      Returns:
      self
      See Also:
      HttpURLConnection.html#instanceFollowRedirects
    • getInputStream

      public InputStream getInputStream()
      Returns input stream to read server response from.
      Returns:
      input stream to read server response from.
    • headers

      public Map<String,​List<String>> headers()
      Returns HTTP headers as sent by server.
      Returns:
      HTTP headers as sent by server.
    • responseCode

      public int responseCode()
      Returns HTTP response code.
      Returns:
      HTTP response code.
    • responseMessage

      public String responseMessage()
      Returns response message from server, such as "OK", or "Created", etc.
      Returns:
      response message from server, such as "OK", or "Created", etc.
    • bytes

      public byte[] bytes()
      Fetches response content from server as bytes.
      Returns:
      response content from server as bytes.
    • text

      public String text()
      Fetches response content from server as String. UTF-8 is used as a default encoding.
      Returns:
      response content from server as String.
    • text

      public String text(String encoding)
      Fetches response content from server as String.
      Parameters:
      encoding - - name of supported charset to apply when reading data.
      Returns:
      response content from server as String.
    • dispose

      public void dispose()
      This method is already called from text() and bytes(), you do not have to call it if you use those methods.

      However, if you use getInputStream(), call this method in those cases when you think you did not read entire content from the stream.

      This method clears all remaining data in connections after reading a response. This will help keep-alive work smoothly.
    • connect

      protected <T extends Request> T connect()
    • basic

      public <T extends Request> T basic(String user, String password)
      Sets a user and password for basic authentication.
      Parameters:
      user - user.
      password - password.
      Returns:
      self.
    • basic

      public <T extends Request> T basic(String user)
      Set a user authentication
      Parameters:
      user - user
      Returns:
      self
    • doConnect

      protected <T extends Request> T doConnect(HttpURLConnection connection) throws IOException
      Throws:
      IOException
    • getMethod

      protected abstract String getMethod()
    • params

      protected Map<String,​String> params()
    • params

      public <T extends Request> T params(String... namesAndValues)
      Convenience method to add multiple parameters to the request.

      Names and values alternate: name1, value1, name2, value2, etc.
      Parameters:
      namesAndValues - names/values of multiple fields to be added to the request.
      Returns:
      self
    • param

      public <T extends Request> T param(String name, String value)
      Adds a parameter to the request as in a HTML form.
      Parameters:
      name - name of parameter
      value - value of parameter
      Returns:
      self
    • jsonMap

      public JSONMap jsonMap(String encoding)
      Converts a server response to an instance of JSONMap.
      Parameters:
      encoding - pass to specify encoding. Use StandardCharsets name() method to get provide specific charsets.
      Returns:
      instance of JSONMap
    • jsonMap

      public JSONMap jsonMap()
      Converts a server response to an instance of JSONMap. UTF-8 is used as a default encoding.
      Returns:
      instance of JSONMap
    • jsonList

      public JSONList jsonList()
      Converts a server response to an instance of JSONList.
      Returns:
      instance of JSONList
    • jsonList

      public JSONList jsonList(String encoding)
      Converts a server response to an instance of JSONList.
      Parameters:
      encoding - pass to specify encoding. Use StandardCharsets name() method to get provide specific charsets.
      Returns:
      instance of JSONList