skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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 in req object. After that @Req decorator can access req.userId otherwise there is no req.userId.

    Now lets talk about postman testing.

    • Firstly you need to copy the token. (from cookie/ from api response)
    • Next Go to the Headers tab in postman and Add a new key-value pair( Key: Authorization, Value: Bearer ) and send the request
    • After setting the Authorization header, you should now be able to extract the token, validate it, and populate the @Request() object with the user’s context, including req.userId.
      postman-test-image
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search