Request
Request API provides methods to access and manipulate incoming HTTP requests in your application. You can use this API to get information about the request, such as headers
, params
, and data
sent with it.
API
Example request
curl --location 'http://localhost:3000/api/register?rol=admin' \
--header 'Content-Type: application/json' \
--data-raw '{
"firstName": "Nguyen",
"lastName": "Nguyen",
"age": 24
}'
all()
Returns all request data (including both query string
and body
).
const requestData = request.all();
// requestData = { role: 'admin', firstName: 'Nguyen', lastName: 'Nguyen', age: 24 }
query()
Gets all parameters in the query string
of the request.
const queryParams = request.query();
// queryParams = { role: 'admin' }
input(key: string | string[])
Gets the value of a specific field from the request data.
const firstName = request.input('firstName');
// firstName = 'Nguyen'
const selectedData = request.input(['firstName', 'age']);
// selectedData = { firstName: 'Nguyen', age: 24 }
only(keys: string[])
Returns a portion of the request data based on the specified fields.
const requestData = request.only(['firstName', 'lastName']);
// requestData = { firstName: 'Nguyen', lastName: 'Nguyen' }
except(keys: string[])
Returns all request data except the specified fields.
const requestData = request.except(['age']);
// requestData = { role: 'admin', firstName: 'Nguyen', lastName: 'Nguyen' }
- headers()
Returns all request headers.
const headers = request.headers();
// headers = { 'content-type': 'application/json', ... }
header(key: string, defaultValue?: string)
Gets the value of a specific header from the request.
const contentType = request.header('Content-Type');
// contentType = 'application/json'
const nonExistentHeader = request.header('Non-Existent', 'default-value');
// nonExistentHeader = 'default-value'
param(key: string)
Gets the value of a specific parameter from the query string
.
const role = request.param('rol');
// role = 'admin'
has(key: string | string[])
Checks whether one or more fields exist in the request data.
const hasRole = request.has('role');
// hasRole = true
const hasNonExistentField = request.has('nonExistentField');
// hasNonExistentField = false
const hasMultipleFields = request.has(['firstName', 'age']);
// hasMultipleFields = true
const hasSomeFields = request.has(['firstName', 'nonExistentField']);
// hasSomeFields = false
hasAny(key: string | string[])
Checks whether at least one of the fields exists in the request data.
const hasAnyRole = request.hasAny(['role', 'nonExistentField']);
// hasAnyRole = true
const hasAnyNonExistent = request.hasAny(['nonExistentField1', 'nonExistentField2']);
// hasAnyNonExistent = false
protocol()
Gets the protocol of the request (HTTP or HTTPS).
const protocol = request.protocol();
// protocol = 'http://'
hasHeader(key: string)
Checks whether a specific header exists in the request.
const hasContentType = request.hasHeader('Content-Type');
// hasContentType = true
const hasNonExistentHeader = request.hasHeader('Non-Existent-Header');
// hasNonExistentHeader = false
isMethod(method: string)
Checks whether the request method matches the given method.
const isPost = request.isMethod('POST');
// isPost = true
const isGet = request.isMethod('GET');
// isGet = false
path()
Gets the path of the request.
const path = request.path();
// path = '/api/register'
url()
Gets the full URL of the request.
const fullUrl = request.url();
// fullUrl = 'http://localhost:3000/api/register'
method()
Gets the HTTP method of the request.
const method = request.method();
// method = 'POST'
validated()
Gets validated data from the request.
const validatedData = request.validated();
// validatedData = { firstName: 'Nguyen', lastName: 'Nguyen', age: 24 }