Interface HttpRequest
- All Superinterfaces:
AttributeAware
Interface that abstracts out the common request methods used by the system dispatcher.
Example of GET method
Let's take a look at an example HTTP GET request (no body):
GET /api/path/to/hello.html?param1=value1¶m2 HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: something.example.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
When this request is received, the HttpRequest
object contains the following values:
getMethod()
-> RequestMethod.GETgetPath()
-> "/api/path/to/hello.html"getClientAddress()
-> 192.168.99.113 (for example)getParameter("param1")
-> "value1"getParameter("param2")
-> ""getParameter("param3")
-> nullgetParameters()
-> {["param1", "value1"], ["param2", ""]}getHeader("Host")
-> "something.example.com"getHeader("Bogus")
-> nullgetHeaders("Accept-Encoding")
-> ["gzip", "deflate"]getHeaderNames()
-> ["User-Agent", "Host", "Accept-Language", "Accept-Encoding", "Connection"]getInputStream()
-> [empty]getJsonBody()
-> [empty]
Example of POST method
In this example, we look at an example HTTP POST request (has body):POST /api/user HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: mysite.com Content-Type: application/json Content-Length: [length] Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive {"userId": "123", "firstName": "Fred", "lastName", "Flintstone"} // this is the bodyHere's what the "body" methods return:
getInputStream()
-> raw stream of bytesgetJsonBody()
-> the body in JsonNode object
- Since:
- 1.0
- Version:
- 2022-08-30
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns the IP address of the client.Returns the first value for the given header.Returns a list of header names.getHeaders
(String name) Returns a list of values for the specified header.Returns the input stream for the body of the request.Returns the input for the body of the request in pre-parsed form.Returns the HTTP method of the request.getParameter
(String name) Gets the first query parameter with the specified name.Gets all query parameters for the request.getPath()
Returns the path portion of the request.default String
Return the src address if the request arrived over a routed websocket.default boolean
Return true if this request arrived over a routed websocketMethods inherited from interface com.tccc.kos.commons.util.AttributeAware
getAttribute, removeAttribute, setAttribute
-
Method Details
-
getMethod
RequestMethod getMethod()Returns the HTTP method of the request.- Returns:
- the method of the request
-
getPath
String getPath()Returns the path portion of the request.- Returns:
- path of the request
-
getClientAddress
InetSocketAddress getClientAddress()Returns the IP address of the client.- Returns:
- client ip address
-
getParameter
Gets the first query parameter with the specified name.- Parameters:
name
- the parameter name- Returns:
- the first parameter value or null
-
getParameters
MultiValueMap<String,String> getParameters()Gets all query parameters for the request.- Returns:
- the parameters
-
getHeader
Returns the first value for the given header.- Parameters:
name
- the header name- Returns:
- the first value
-
getHeaders
Returns a list of values for the specified header.- Parameters:
name
- the header name- Returns:
- list of values
-
getHeaderNames
Collection<String> getHeaderNames()Returns a list of header names.- Returns:
- list of header names
-
getInputStream
Returns the input stream for the body of the request. This can only be read once.- Returns:
- input stream to the request body
- Throws:
IOException
-
getJsonBody
JsonNode getJsonBody()Returns the input for the body of the request in pre-parsed form. This is only used for requests that bypass networking (API calls), which basically performs object-to-object mapping, but without going to string.- Returns:
- input body in parsed form
-
isWebsocket
default boolean isWebsocket()Return true if this request arrived over a routed websocket -
getWebsocketSrcAddr
Return the src address if the request arrived over a routed websocket.
-