13. HTTP Request Parameters
Nekonote::Request is the class to manage HTTP Request Parameters such as form data, query string, URL path parameters.
Nekonote::Request Class
An object of Nekonote::Request class will be set to @request in Handler classes.
The following is the list of public instance methods which defined in Nekonote::Request class.
| params | Returns values by the given name from the POST data or the query string. |
| query_string | Returns values by the given name from the query string. |
| post_data | Returns values by given name from the POST data. |
| path_params | Returns values by the given name from the URL path parameters. |
| payload | Returns the payload as string. |
| json_payload | Returns the payload as hash. |
| path | Returns the request path. |
| uri | Returns the request URI. |
| method | Returns the HTTP request method. |
| valid_str? | Checks if the value of given parameter name is valid string. |
| having? | Checks if the value of given parameter name exist in the POST data or the query string. |
params(name = nil)
Returns values by the given name from the POST data or the query string.
URL path parameters or payload can not be reference by this method.
When HTTP request method is GET this method finds the value from the query string, when POST from the POST data.
| parameter | type | required | description |
|---|---|---|---|
| name | string or symbol | no | A parameter name to get. |
Return Values
It returns hash when just calling @request.params.
It return string when value found by the given parameter name when not returns nil.
query_string(name = nil)
This method is almost the same as params. The only deference is query_string tries to find the values from the query string.
post_data(name = nil)
This method is almost the same as params. The only deference is post_data tries to find the values from the POST data.
path_params(name = nil)
This method is almost the same as params. The only deference is that path_params tries to find the values from the URL path parameters.
payload
Returns the payload.
Return Values
It returns string. In case of there is not any payload it returns nil instead.
json_payload
Returns the payload as Hash object when the payload is valid json data.
Return Values
It returns hash when the payload is valid json data and succeeded parsing.
It returns nil when there is not any payload.
It returns false if unable to parse the payload as json.
path
Returns the request path.
Return Values
It returns string.
uri
Returns the request URI.
Return Values
It returns string.
method
Returns the HTTP request method.
Return Values
It returns string.
valid_str?(name)
Checks if the value of given parameter name is string and is not empty which in the POST data or the query string.
| parameter | type | required | description |
|---|---|---|---|
| name | string or symbol | yes | A parameter name. |
Return Values
It returns true when the type of inspected value is string and is not empty string. If not it returns false.
having?(name)
Checks if the value of given parameter name exist in the POST data or the query string.
| parameter | type | required | description |
|---|---|---|---|
| name | string or symbol | yes | A parameter name. |
Return Values
It returns true when the specified parameter exists in POST data or query string.
Getting Payload
When there is some request body in a request, you may reference it by json_payload.
preference/your_environment/route.yml
ExampleHandler:
path: /example
execute: index
method: POST
handler/example.rb
class ExampleHandler < BaseHandler
def example
logwrite @request.json_payload
end
end
When accessing http://example.com/example with POST method and with Content-Type: application/json header and with the request body below,
{
"id": 2002,
"name": "example",
"type": 1
}
@request.json_payload returns {"id"=>2002, "name"=>"example", "type"=>1} in hash.
In order to reference payload which is not json data, please use @request.payload instead. It returns no parsed request body as json data.