I am testing a NestJS application using Postman.
In NestJS we have @Request() decorator(part of @nestjs/common), in which I have the context of the currently logged-in user of my application, which I can access using req.userId
or req['userId']
It works fine from the front end, and I get the currently logged-in user context correctly.
I am using the Bearer token, and my sign-in API correctly gives me the token and refresh token.
How can this(the NestJS @Request object) be passed from the postman while testing, as it’s neither part of @Body, @Param or @Query?
2
Answers
A client simply needs to make a HTTP Request and if the server the request is made to cannot handle the request, it is not a HTTP compliant server.
What do you mean "pass from Postman"? The question is based on a false premise. Postman is just another HTTP client like your browser is. It should construct a complete HTTP request in accordance with what the server endpoint accepts and then the server should process that and return a response.
Check with your server to know what it requires of various HTTP requests at various endpoints. Construct one with everything the server requests and send to the server. It should process your request and return a response.
There is no
req.userId
in request object until you are set the key value. The process usually done by developers to pass the token by headers to verify and set the token information inreq
object. After that@Req
decorator can accessreq.userId
otherwise there is noreq.userId
.Now lets talk about postman testing.
Authorization
, Value: Bearer ) and send the requestAuthorization
header, you should now be able to extract the token, validate it, and populate the @Request() object with the user’s context, includingreq.userId
.