Creates a new Request instance wrapping the native Node.js HTTP request
Native Node.js incoming message instance
Native Node.js server response instance
Encryption module for cookie and URL signing
Request configuration options
Query string parser instance
Parsed URL with query string stored as a string and decode flag
The pathname portion of the URL
The query string portion of the URL
Flag indicating whether parameters should be decoded
Optional
ctxHTTP context reference - creates a circular reference when set by the context
Native Node.js incoming message instance
Native Node.js server response instance
Returns the request id from the x-request-id
header. The
header is untouched, if it already exists.
The request ID or undefined if not found/generated
Set initial request body. A copy of the input will be maintained as the original request body. Since the request body and query string is subject to mutations, we keep one original reference to flash old data (whenever required).
This method is supposed to be invoked by the body parser and must be called only
once. For further mutations make use of updateBody
method.
Parsed request body data
Update the request body with new data object. The all
property
will be re-computed by merging the query string and request
body.
New request body data to set
Update the request raw body. Bodyparser sets this when unable to parse the request body or when request is multipart/form-data.
Raw request body as string
Update the query string with the new data object. The all
property
will be re-computed by merging the query and the request body.
New query string data to set
Returns route params
Object containing route parameters
Returns the query string object by reference
Object containing parsed query string parameters
Returns reference to the request body
Object containing parsed request body
Returns reference to the merged copy of request body and query string
Object containing merged request body and query parameters
Returns reference to the merged copy of original request query string and body
Object containing original merged request data
Returns the request raw body (if exists), or returns null
.
Ideally you must be dealing with the parsed body accessed using [[input]], [[all]] or
[[post]] methods. The raw
body is always a string.
Raw request body as string or null if not set
Returns value for a given key from the request body or query string.
The defaultValue
is used when original value is undefined
.
Key to lookup in request data
Optional
defaultValue: anyDefault value when key is not found
Value from request data or default value
Get value for specified keys.
Array of keys to include in the result
Object with only the specified keys from request data
Returns the request HTTP method by taking method spoofing into account.
Method spoofing works when all of the following are true.
app.http.allowMethodSpoofing
config value is true._method
.POST
.HTTP method (potentially spoofed)
Returns a copy of headers as an object
Object containing all HTTP headers
Returns value for a given header key. The default value is
used when original value is undefined
.
Header name to lookup
Optional
defaultValue: anyDefault value when header is not found
Header value or default value if not found
Returns the ip address of the user. This method is optimize to fetch ip address even when running your AdonisJs app behind a proxy.
You can also define your own custom function to compute the ip address by
defining app.http.getIp
as a function inside the config file.
{
http: {
getIp (request) {
// I am using nginx as a proxy server and want to trust 'x-real-ip'
return request.header('x-real-ip')
}
}
}
You can control the behavior of trusting the proxy values by defining it
inside the config/app.js
file.
{
http: {
trustProxy: '127.0.0.1'
}
}
The value of trustProxy is passed directly to proxy-addr
Client IP address
Returns an array of ip addresses from most to least trusted one. This method is optimize to fetch ip address even when running your AdonisJs app behind a proxy.
You can control the behavior of trusting the proxy values by defining it
inside the config/app.js
file.
{
http: {
trustProxy: '127.0.0.1'
}
}
The value of trustProxy is passed directly to proxy-addr
Array of IP addresses from most to least trusted
Returns the request protocol by checking for the URL protocol or
X-Forwarded-Proto
header.
If the trust
is evaluated to false
, then URL protocol is returned,
otherwise X-Forwarded-Proto
header is used (if exists).
You can control the behavior of trusting the proxy values by defining it
inside the config/app.js
file.
{
http: {
trustProxy: '127.0.0.1'
}
}
The value of trustProxy is passed directly to proxy-addr
Request protocol ('http' or 'https')
Returns a boolean telling if request is served over https
or not. Check [[protocol]] method to know how protocol is
fetched.
True if request is served over HTTPS
Returns the request host. If proxy headers are trusted, then
X-Forwarded-Host
is given priority over the Host
header.
You can control the behavior of trusting the proxy values by defining it
inside the config/app.js
file.
{
http: {
trustProxy: '127.0.0.1'
}
}
The value of trustProxy is passed directly to proxy-addr
Request host or null if not found
Returns the request hostname. If proxy headers are trusted, then
X-Forwarded-Host
is given priority over the Host
header.
You can control the behavior of trusting the proxy values by defining it
inside the config/app.js
file.
{
http: {
trustProxy: '127.0.0.1'
}
}
The value of trustProxy is passed directly to proxy-addr
Request hostname (without port) or null if not found
Returns an array of subdomains for the given host. An empty array is
returned if [[hostname]] is null
or is an IP address.
Also www
is not considered as a subdomain
Array of subdomains (excluding www)
Returns a boolean telling, if request X-Requested-With === 'xmlhttprequest'
or not.
True if request is an AJAX request
Returns a boolean telling, if request has X-Pjax
header
set or not
True if request is a PJAX request
Find if the current HTTP request is for the given route or the routes
Route name, pattern, or handler reference to match
True if the request matches any of the given route identifiers
Returns the best matching content type of the request by matching against the given types.
The content type is picked from the content-type
header and request
must have body.
The method response highly depends upon the types array values. Described below:
Type(s) | Return value |
---|---|
['json'] | json |
['application/*'] | application/json |
['vnd+json'] | application/json |
Array of content types to match against
Best matching content type or null if no match
Returns the best type using Accept
header and
by matching it against the given types.
If nothing is matched, then null
will be returned
Make sure to check accepts package docs too.
Array of types to match against Accept header
Best matching accept type or null if no match
Return the types that the request accepts, in the order of the client's preference (most preferred first).
Make sure to check accepts package docs too.
Array of accepted types in preference order
Returns the best language using Accept-language
header
and by matching it against the given languages.
If nothing is matched, then null
will be returned
Make sure to check accepts package docs too.
Array of languages to match against Accept-Language header
Best matching language or null if no match
Return the languages that the request accepts, in the order of the client's preference (most preferred first).
Make sure to check accepts package docs too.
Array of accepted languages in preference order
Returns the best charset using Accept-charset
header
and by matching it against the given charsets.
If nothing is matched, then null
will be returned
Make sure to check accepts package docs too.
Array of charsets to match against Accept-Charset header
Best matching charset or null if no match
Return the charsets that the request accepts, in the order of the client's preference (most preferred first).
Make sure to check accepts package docs too.
Array of accepted charsets in preference order
Returns the best encoding using Accept-encoding
header
and by matching it against the given encodings.
If nothing is matched, then null
will be returned
Make sure to check accepts package docs too.
Array of encodings to match against Accept-Encoding header
Best matching encoding or null if no match
Return the encodings that the request accepts, in the order of the client's preference (most preferred first).
Make sure to check accepts package docs too.
Array of accepted encodings in preference order
Returns a boolean telling if request has body
True if request contains a body
Returns a boolean telling if the new response etag evaluates same
as the request header if-none-match
. In case of true
, the
server must return 304
response, telling the browser to
use the client cache.
You won't have to deal with this method directly, since AdonisJs will
handle this for you when http.etag = true
inside config/app.js
file.
However, this is how you can use it manually.
const responseBody = view.render('some-view')
// sets the HTTP etag header for response
response.setEtag(responseBody)
if (request.fresh()) {
response.sendStatus(304)
} else {
response.send(responseBody)
}
True if client cache is fresh (should return 304)
Opposite of [[fresh]]
True if client cache is stale (should send new response)
Returns all parsed and signed cookies. Signed cookies ensures that their value isn't tampered.
Object containing all parsed cookies
Returns value for a given key from signed cookies. Optional defaultValue is returned when actual value is undefined.
Cookie name to lookup
Optional
defaultValue: stringDefault value when cookie is not found
Cookie value or default value if not found
Returns value for a given key from encrypted cookies. Optional defaultValue is returned when actual value is undefined.
Cookie name to lookup
Optional
defaultValue: stringDefault value when cookie is not found
Decrypted cookie value or default value if not found
Returns value for a given key from unsigned cookies. Optional defaultValue is returned when actual value is undefined.
Cookie name to lookup
Optional
options: { defaultValue?: string; encoded?: boolean }Options object with defaultValue and encoded flag
Plain cookie value or default value if not found
Returns value for a given key from unsigned cookies. Optional defaultValue is returned when actual value is undefined.
Cookie name to lookup
Optional
defaultValue: stringOptional
encoded: booleanPlain cookie value or default value if not found
Returns a boolean telling if a signed url has a valid signature or not.
Optional
purpose: stringOptional purpose for signature verification
True if the signed URL has a valid signature
Serializes request to JSON format
Object representation of the request
toJSON copy of the request
JSON representation of the request
HTTP Request class exposes the interface to consistently read values related to a given HTTP request. The class is wrapper over IncomingMessage and has extended API.
You can access the original IncomingMessage using
request.request
property.