Class HttpRequestEvent
java.lang.Object
net.risingworld.api.events.Event
net.risingworld.api.events.general.HttpRequestEvent
Called when a HTTP request is received by the webserver, targeting a route which
was registered by a plugin (see
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!
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!
-
Nested Class Summary
Nested Classes -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]getBody()Gets the request body as a byte array.Gets the request body as a UTF-8 string.Gets the value of a particular request header, or null if no such header is present.Gets the HTTP method of this request.getPath()Gets the requested path (without query string), for example "/plugins/myplugin/status".getQuery()Gets the raw query string of this request (the part after the "?"), or an empty string if the request has no query string.Gets the query parameters of this request as a map.Gets the remote address (IP) of the client which sent this request.booleanisSecure()Gets whether or not this request was received over a secure (HTTPS) connection.voidsetContentType(String contentType) Sets the content type (i.e the "Content-Type" header) of the response, for example "application/json" or "text/html".voidsetResponseBody(byte[] body) Sets the response body as raw binary data.voidsetResponseBody(String body) Sets the response body as a string (encoded as UTF-8).voidsetResponseCode(int code) Sets the HTTP status code of the response (e.g 200 for "OK", 404 for "Not Found").voidsetResponseHeader(String name, String value) Sets a custom response header.
-
Method Details
-
getMethod
Gets the HTTP method of this request.- Returns:
- the HTTP method (e.g
HttpRequestEvent.HttpMethod.GET).
-
getPath
Gets the requested path (without query string), for example "/plugins/myplugin/status".- Returns:
- the requested path.
-
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
Gets the query parameters of this request as a map. This is a convenience method which parses the result ofgetQuery().- Returns:
- a (possibly empty) map containing the query parameters. Never null.
-
getHeader
-
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
-
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
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
-
setResponseBody
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.
-