Interface Request
- All Known Implementing Classes:
HttpRequest
public interface Request
The Request interface represents an HTTP request received by the server.
It provides methods to retrieve information about the request, such as the
request body, headers, parameters, URL, and more.
This interface also includes methods to control the flow of request handling,
such as indicating whether the request should continue to be handled by the
next middleware or route.
Implementations of this interface should be able to handle both HTTP/1.1 and HTTP/2 requests, and should be able to handle requests with or without a body.
Users of this interface should be able to obtain information about the request by calling the various accessor methods. Implementations of this interface should provide default implementations for all of these methods, so that users only need to override the methods that they need.
Example usage:
Request request = ...; // create an instance of the request object
String method = request.method(); // get the HTTP method (GET, POST, etc.)
String path = request.path(); // get the path component of the URL
String header = request.header(name); // get a request header
String contentType = request.contentType(); // get the value of the Content-Type header
String name = request.query("name"); // get the value of the "name" query parameter
String body = request.body(ClassNAME.class); // get instance of class <code>ClassNAME</code> from the exist body.
...
-
Method Summary
Modifier and TypeMethodDescriptionbody()
Gets the body of the request as a string.Parses the body of the request and maps it to an instance of the specified class.Returns the name of the character encoding used in the body of this request.Returns the MIME type of the body of the request, or null if the type is not known.Returns the context path of the request.Parses the body of the request as form data and returns it as a map.getAttribute
(String name) Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.long
getDateHeader
(String name) Returns the value of the specified request header as a long value that represents a Date object.Returns the value of the specified request header as a string.hostname()
Returns the hostname of the server that received the current request.jsonBody()
Gets the body of the request as a JSON string.method()
Returns the HTTP request method (e.g.boolean
next
(boolean isContinue) Sets the value indicating whether the request should continue to be handled by the next middleware or route.Returns the value of the specified request parameter as a String, or null if the parameter does not exist.path()
Returns the path of the current request.pattern()
Returns the path pattern of the current request.int
port()
Returns the port number on which the server is listening for requests.protocol()
Returns the HTTP protocol used for the request (e.g.Returns the value of the specified query parameter as a String, or null if the parameter does not exist.removeAttribute
(String name) Removes the attribute with the given name from this request.jakarta.servlet.http.HttpSession
session()
Returns the HttpSession object associated with this request.void
setAttribute
(String name, Object obj) Stores an attribute in this request.void
Sets the body of this Request.boolean
Returns the value indicating whether the request should continue to be handled by the next middleware or route.url()
Returns the full URL of the current request, including the protocol, hostname, port, context path, servlet path, and path.Gets the body of the request in the URL-encoded format.
-
Method Details
-
body
String body()Gets the body of the request as a string.- Returns:
- the body of the request as a string.
-
formDataBody
Parses the body of the request as form data and returns it as a map.- Returns:
- a map containing the form data in the request body.
-
jsonBody
String jsonBody()Gets the body of the request as a JSON string.- Returns:
- the body of the request as a JSON string.
-
urlEncodedFormatBody
String urlEncodedFormatBody()Gets the body of the request in the URL-encoded format.- Returns:
- the body of the request in the URL-encoded format.
-
body
Parses the body of the request and maps it to an instance of the specified class.- Parameters:
clazz
- the class to map the request body to.- Returns:
- an instance of the specified class containing the request body data.
- Throws:
IllegalArgumentException
- if the request body cannot be mapped to the specified class.
-
method
String method()Returns the HTTP request method (e.g. "GET", "POST", "PUT", etc.).- Returns:
- the HTTP request method
-
protocol
String protocol()Returns the HTTP protocol used for the request (e.g. "HTTP/1.1").- Returns:
- the HTTP protocol
-
params
Returns the value of the specified request parameter as a String, or null if the parameter does not exist.- Parameters:
name
- the name of the parameter to retrieve- Returns:
- the value of the parameter, or null if the parameter does not exist
-
query
Returns the value of the specified query parameter as a String, or null if the parameter does not exist.- Parameters:
name
- the name of the query parameter to retrieve- Returns:
- the value of the query parameter, or null if the parameter does not exist
-
header
Returns the value of the specified request header as a string. If the request did not include a header with the specified name, this method returns null.- Parameters:
headerName
- the name of the header- Returns:
- a string containing the value of the specified request header, or null if the request does not have a header with that name
-
getDateHeader
Returns the value of the specified request header as a long value that represents a Date object. Use this method with headers that contain dates, such as If-Modified-Since.- Parameters:
name
- the name of the header- Returns:
- a long value representing the date specified in the header expressed as the number of milliseconds since January 1, 1970 GMT, or -1 if the named header was not included with the request
-
contentType
String contentType()Returns the MIME type of the body of the request, or null if the type is not known. This method returns the value of the Content-Type header field if it exists and is accessible; otherwise, it returns null.- Returns:
- a string containing the name of the MIME type of the request, or null if the type is not known
-
bodyEncoding
String bodyEncoding()Returns the name of the character encoding used in the body of this request. This method returns null if the request does not specify a character encoding.- Returns:
- a string containing the name of the character encoding, or null if the request does not specify a character encoding
-
contextPath
String contextPath()Returns the context path of the request. The context path is the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. If this method is called on a request that was dispatched to a servlet using a RequestDispatcher, the context path returned is the context path of the original request, not the context path of the dispatch target.- Returns:
- a string containing the context path of the request
-
path
String path()Returns the path of the current request. The path is the part of the URL after the domain name and context path. For example, if the full URL is "http://example.com/context/servlet-path/path/stuffs", then the path returned by this method would be "/path/stuffs".- Returns:
- the path of the current request
-
hostname
String hostname()Returns the hostname of the server that received the current request.- Returns:
- the hostname of the server that received the current request
-
port
int port()Returns the port number on which the server is listening for requests.- Returns:
- the port number on which the server is listening for requests
-
url
String url()Returns the full URL of the current request, including the protocol, hostname, port, context path, servlet path, and path.- Returns:
- the full URL of the current request
-
pattern
String pattern()Returns the path pattern of the current request.- Returns:
- the path pattern of the request, for example "/path/:id/src".
-
next
boolean next(boolean isContinue) Sets the value indicating whether the request should continue to be handled by the next middleware or route.- Parameters:
isContinue
- true if the request should continue, false otherwise.
-
shouldContinue
boolean shouldContinue()Returns the value indicating whether the request should continue to be handled by the next middleware or route. By default, this value is false.- Returns:
- true if the request should continue, false otherwise.
-
setBody
Sets the body of this Request. If the given body is null, the effect is the same as calling setBody("") to set an empty body.- Parameters:
body
- a String representing the body of this Request.
-
setAttribute
Stores an attribute in this request. Attribute names should follow the same conventions as package names. Names beginning with java.*, javax.*, and com.sun.*, are reserved for use by the Servlet specification. If the object passed in is null, the effect is the same as calling removeAttribute(String).- Parameters:
name
- the name of the attribute to be stored.obj
- the object to be stored as the attribute value.
-
getAttribute
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. Attribute names should follow the same conventions as package names. This specification reserves names matching java.*, javax.*, and sun.*.- Parameters:
name
- the name of the attribute to retrieve.- Returns:
- the value of the named attribute, or null if no attribute of the given name exists.
-
removeAttribute
Removes the attribute with the given name from this request. If the request does not have an attribute with the given name, this method does nothing.- Parameters:
name
- the name of the attribute to remove.- Returns:
- the value of the removed attribute, or null if no attribute of the given name existed.
-
session
jakarta.servlet.http.HttpSession session()Returns the HttpSession object associated with this request. If the request does not have a session, this method creates one. This method never returns null and should not create a new session if the request does not have a valid session ID.- Returns:
- the HttpSession associated with this request
-