Requests
Introduction
The Requests class in Vania offers a convenient way to work with the incoming HTTP requests that your application receives. It acts as a bridge between your application and the data sent by the client's browser.
With the Requests class, you can easily access information about the current request, such as the data submitted by the user, any files uploaded with the request, and other relevant details.
By utilizing the Requests class, developers can efficiently retrieve and process user input, handle file uploads, and perform various operations based on the incoming request data. This object-oriented approach simplifies the handling of HTTP requests within your application, enabling you to build responsive and interactive web experiences without unnecessary complexity.
Accessing the Request
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Request class on your route closure or controller method. The incoming request instance will automatically be injected
class UserController extends Controller
{
Future store(Request request)
{
var name = request.input('name');
return Response.json({'name':name});
}
}
As mentioned, you may also type-hint the Request class on a route closure.
Route.get('/', (Request request) {
// ...
});
Request Method
Retrieving the Request Path
The path method returns the request's path information. So, if the incoming request is targeted at [http://example.com/foo/bar], the path method will return foo/bar:
Route.get('/', (Request request) {
final path = request.path;
});
Retrieving the Request Host
You may retrieve the "host" of the incoming request via the host, httpHost
Route.get('/', (Request request) {
final host = request.host;
});
Retrieving the Request Url
To retrieve the URL for the incoming request you may use the url methods. The url method will return the URL without the query string
Route.get('/', (Request request) {
final url = request.url;
});
Retrieving the Request Method
The method method will return the HTTP verb for the request. You may use the isMethod method to verify that the HTTP verb matches a given string:
Route.get('/', (Request request) {
var mesthod = request.method;
if (request.isMethod('post')) {
// ...
}
});
Request Headers
You may retrieve a request header from the Request If the header is not present on the request, null will be returned. However, the header method accepts an optional second argument that will be returned if the header is not present on the request
Route.get('/', (Request request) {
final header = request.header('X-Header-Name');
final header = request.header('X-Header-Name','Defualt Header');
});
Request IP Address
The ip method may be used to retrieve the IP address of the client that made the request to your application:
Route.get('/', (Request request) {
final ip = request.ip;
});
Retrieving Input
Retrieving All Input Data
You may retrieve all of the incoming request's input data as an List using the all method. This method may be used regardless of whether the incoming request is from an HTML form or is an XHR request:
final allRequest = request.all();
Retrieving an Input Value
Using a few simple methods, you may access all of the user input from your Request instance without worrying about which HTTP verb was used for the request. Regardless of the HTTP verb, the input method may be used to retrieve user input:
final name = request.input('name');
You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:
final name = request.input('name','Vania');
You may call the input method without any arguments in order to retrieve all of the input values as an associative list:
final name = request.input();
Retrieving Input From the Query String
While the input method retrieves values from the entire request payload (including the query string), the query method will only retrieve values from the query string:
final query = request.query('name')
If the requested query string value data is not present, the second argument to this method will be returned:
final defaultQuery = request.query('name','John')
You may call the query method without any arguments in order to retrieve all of the query string values as an associative list:
final all = request.query()
Retrieving Stringable Input Values
Instead of retrieving the request's input data as a primitive string, you may use the string method to retrieve the request data as String
String name = request.string('name');
Retrieving Boolean Input Values
bool archived = request.boolean('archived');