AdonisJs makes it super easy to access HTTP request information. All controller methods and route closures receive an instance of the Request class, which is a sugared layer on top of Node.js HTTP request.
You can access Node.js raw request object as request.request.
Let's take the classic case of reading request body for a given HTTP request.
Below is the list of all the available methods on the Request instance.
Returns all values extracted from query strings and the request body.
Returns the value of query strings and request body for a given key. If the value does not exist, the default value will be returned.
Similar to all, but returns an object with cherry picked values of defined keys. null will be returned for non-existing keys.
Returns a serialized object of query strings.
Returns a serialized object of the request body.
Tells whether the request is fresh or not by checking ETag and expires header.
Returns most trusted IP address for a given request. If your application is behind a proxy server like Nginx, make sure to enable http.trustProxy inside the config/app.js file.
Returns an array of IP addresses sorted from most to least trusted.
Tells whether the request is served over HTTPS or not.
Returns an array of subdomains for a given URL. For example, api.example.org will have the subdomain as ['api'].
Returns whether the current request is made using Ajax(XMLHttpRequest) or not.
Pjax is a hybrid ajax request. If you are from Ruby on Rails world, it is quite similar to Turbolinks.
Returns request current URL. It will trim query string.
Returns route parameter for a given key. Learn more about route parameters here.
Returns all params as an object.
Returns current format for a given request. In order to make it work, you need to define route formats.
Returns a boolean indicating whether the current request URL matches any of the given patterns.
Returns whether the request has the body or not.
Returns value for a given header key or returns the default value.
Returns all headers as an object.
Quite often applications have requirements of saving multiple entries to the database using HTML forms. Let's take an example of saving multiple users.
Above we defined the email and password as an array so that we can submit multiple users within a single request and the input on the server will look quite similar the below format.
Until this point, the form is doing what it is supposed to do. Whereas the data received by the server is quite hard to process to get it into the right format.
Of course, you can loop through the original input and create a new array as per the expected output, but that seems to be too much for a general use case. AdonisJs makes the entire process seamless by introducing a helper method called collect.
Content Negotiation is a way to find the best response type for a given request. The end-user makes use of HTTP headers to define the response type they are expecting from the server.
You can also make use of Routes to define explicit return types. Learn more about content negotiation via routes.
Returns whether a request is one of the given types. This method will parse the request Content-type header.
Checks the Accept header to negotiate the best response type for a given HTTP request.
Quite often you have the requirement of extending the Request prototype by attaching new methods. Same can be done by defining a macro on the Request class.
Defined macros can be used like any other request method.