Class HttpRequestEvent

java.lang.Object
net.risingworld.api.events.Event
net.risingworld.api.events.general.HttpRequestEvent

public final class HttpRequestEvent extends Event
Called when a HTTP request is received by the webserver, targeting a route which was registered by a plugin (see Plugin.registerWebserverHandler(String)).

This event allows the plugin to read the incoming request and provide a response (status code, headers and body). If the plugin does not set a response, the webserver will return "404 Not Found" by default.

Please note that the Webserver is only active for dedicated servers!
  • Method Details

    • getMethod

      public HttpRequestEvent.HttpMethod getMethod()
      Gets the HTTP method of this request.
      Returns:
      the HTTP method (e.g HttpRequestEvent.HttpMethod.GET).
    • getPath

      public String getPath()
      Gets the requested path (without query string), for example "/plugins/myplugin/status".
      Returns:
      the requested path.
    • getQuery

      public String getQuery()
      Gets the raw query string of this request (the part after the "?"), or an empty string if the request has no query string. Example: for the url "/plugins/myplugin/status?foo=1&bar=2" this returns "foo=1&bar=2".
      Returns:
      the raw query string (without leading "?").
    • getQueryParameters

      public Map<String,String> getQueryParameters()
      Gets the query parameters of this request as a map. This is a convenience method which parses the result of getQuery().
      Returns:
      a (possibly empty) map containing the query parameters. Never null.
    • getHeader

      public String getHeader(String name)
      Gets the value of a particular request header, or null if no such header is present. The header name is treated case-insensitively.
      Parameters:
      name - the name of the header (e.g "Content-Type").
      Returns:
      the header value, or null if no such header was found.
    • getBody

      public byte[] getBody()
      Gets the request body as a byte array. For requests without a body (e.g typical GET requests), this returns an empty array.
      Returns:
      the request body as a byte array.
    • getBodyAsString

      public String getBodyAsString()
      Gets the request body as a UTF-8 string. This is a convenience method which decodes the result of getBody() using the UTF-8 charset.
      Returns:
      the request body as a string. If there is no body, an empty string is returned.
    • getRemoteAddress

      public String getRemoteAddress()
      Gets the remote address (IP) of the client which sent this request.
      Returns:
      the remote address of the client.
    • isSecure

      public boolean isSecure()
      Gets whether or not this request was received over a secure (HTTPS) connection.
      Returns:
      true if the request was received over HTTPS, false otherwise.
    • setResponseCode

      public void setResponseCode(int code)
      Sets the HTTP status code of the response (e.g 200 for "OK", 404 for "Not Found"). If not set, the response will use the status code 200 by default (as long as any other response value was set, otherwise a 404 is returned).
      Parameters:
      code - the HTTP status code.
    • setContentType

      public void setContentType(String contentType)
      Sets the content type (i.e the "Content-Type" header) of the response, for example "application/json" or "text/html".
      Parameters:
      contentType - the content type of the response.
    • setResponseHeader

      public void setResponseHeader(String name, String value)
      Sets a custom response header.
      Parameters:
      name - the name of the header.
      value - the value of the header.
    • setResponseBody

      public void setResponseBody(String body)
      Sets the response body as a string (encoded as UTF-8). This replaces any previously set response body.
      Parameters:
      body - the response body.
    • setResponseBody

      public void setResponseBody(byte[] body)
      Sets the response body as raw binary data. This replaces any previously set response body.
      Parameters:
      body - the response body.