Skip to content

Request Parameters

Thiago Bustamante edited this page Jan 20, 2019 · 1 revision

Request Parameters

There are decorators to map parameters to arguments on service methods. Each decorator can map a different kind of parameter on request.

The following decorators are available:

Decorator Description
@PathParam Parameter in requested URL path
@QueryParam Parameter in the query string
@FormParam Parameter in an HTML form
@HeaderParam Parameter in the request header
@CookieParam Parameter in a cookie
@FileParam A File in a multipart form
@FilesParam An array of Files in a multipart form
@Param Parameter in the query string or in an HTML form

Some examples:

@Path("/sample")
class Sample {
   @GET
   test(@QueryParam("limit") limit:number, @QueryParam("skip") skip:number) {
      //...
      // GET http://domain/sample?limit=5&skip=10
   }

   @POST
   test(@FormParam("name") name:string) {
      //...
      // POST http://domain/sample
      // body: name=joe
   }

   @POST
   @Path("upload")
   testUploadFile( @FileParam("myFile") file: Express.Multer.File, 
                   @FormParam("myField") myField: string) {
      //...
      /* POST http://domain/sample/upload
      Content-Type: multipart/form-data; boundary=AaB03x

      --AaB03x
      Content-Disposition: form-data; name="myField"

      Field Value
      --AaB03x
      Content-Disposition: form-data; name="myFile"; filename="file1.txt"
      Content-Type: text/plain

      ... contents of file1.txt ...
      --AaB03x--
      */
   }
}

An argument that has no decorator is handled as a json serialized entity in the request body

@Path("/sample")
class Sample {
   @POST
   test(user: User) {
      //...
      // POST http://domain/sample
      // body: a json representation of the User object
   }
}

The @*Param decorators can also be used on service class properties.

An example:

 @Path("users/:userId/photos")
 class TestService {
   @PathParam('userId')
   userId: string;

   @GET
   getPhoto(@PathParam('photoId')) {
      // Get the photo and return
   }
 }