Parameters
Http Plugin provides a set of decorators implemented through the @axisparkjs/engine package that allow you to extract parameters from the request and validate them. These decorators can be used in your controller methods to easily access and validate request parameters.
| Decorator | Description |
|---|---|
@Context() | Extracts the full context of the current request / execution |
@Request() | Extracts the HTTP request object from the context |
@Response() | Extracts the HTTP response object from the context |
@Body() | Extracts the body from the HTTP request. It can be used to extract the request body, map it to a DTO, and validate it. |
@Path(field: string) | Extracts a specific field from the HTTP request path parameters. If the field is not found, it will return undefined. |
@Query(field: string) | Extracts a specific field from the HTTP request query parameters. If the field is not found, it will return undefined. |
@Header(field: string) | Extracts a specific field from the HTTP request headers. If the field is not found, it will return undefined. |
@Ip() | Extracts the IP address of the client making the request. |
@Session() | Extracts the session object from the HTTP request. If the session options are disabled, it will return undefined. |
@Cookie(field: string) | Extracts a specific field from the HTTP request cookies. If the cookie is not found or the session options are disabled, it will return undefined. |
@UploadedFile(field: string) | Extracts the first file uploaded in a multipart/form-data field. If the field has no file or the fileUpload option is disabled, it will return undefined. See File uploads. |
@UploadedFiles(field?: string) | Extracts the files uploaded in a multipart/form-data field, or every uploaded file when no field is given. If the fileUpload option is disabled, it will return undefined. See File uploads. |
Usage
Here are some examples of how to use the parameter decorators in your controller methods:
@Get()
get(@Context() context: HttpContext, @Request() req: HttpRequest, @Response() res: HttpResponse) {...}
@Get()
getTodos(@Header('authorization') auth: string, @Query('limit') limit: string) {...}
@Get(':index')
getTodoByIndex(@Path('index') index: number, @Query('includeDetails') includeDetails: string) {...}
@Post()
addTodo(@Body() todo: { task: string }) {...}
@Put(':index')
updateTodo(@Path('index') index: string, @Body() updatedTodo: { task: string }) {...}
@Delete(':index')
removeTodo(@Path('index') index: string, @Ip() ip: string) {...}