There is hardly a backend app that doesn't involve some sort of authentication or authorization. Testing can be very tedious because it means you have to always pass in the token returned from any of your endpoints that generate tokens to the header of your other endpoints that require that token to give them authority to access those endpoints.
I will give you an example. Say you developing the backend of a social media app. You want people to be able to register and log in. You also want people to be able to create posts only if they are logged in. How do you know if a person is logged in? Well, there are different methods of handling authentication and authorization but in this article, we will be discussing the use of JSON Web Tokens(JWT). What JWT does is generate a token for a user when they sign in and that token is then passed into the header of any request endpoint that requires authentication and authorization before access, e.g. creating a post, adding a friend, and many more.
The problem with the above style of passing tokens to request is that whenever you restart your server and need to test again and hit the login endpoint, the token generated is different from the last one and this means that you now have to copy that token manually and paste it in any of the endpoints that requires authorization. This might not sound like a big deal if you are dealing with 2 or 3 endpoints that require authorization but imagine you had 25 or more of those endpoints. Things begin to get messed up quickly. The solution to this problem in Postman is to make use of the combination of environment variables and tests. Yes, you can write tests in Postman.
First, Let us talk about environment variables. You use environment variables to store values that you need often to make requests. For example, your base URL. imagine you have fifty requests which of course will contain your base URL plus other endpoints e.g. localhost:8080/login. Here, localhost:8080 is the base URL and is always the same across all endpoints. Now something happened and we need to change our base URL from localhost:8080 to something else, say localhost:9090. This means that you have to change it in all the fifty or more requests that you have but if you are using environment variables, you only need to change it in one place and it reflects everywhere. Example? why not!
Now that we know how to set environment variables manually, let’s talk about how to set them automatically. We can set environment variables automatically and in our case, the token gotten from logging in by writing a test in the request file. Let’s take a look at an example of how to do that.
If you check the sample login request screenshot above, you will see that in the response, the token is stored in a variable name called accessToken. This might be different for you but what is important is that you take note of the name. Now let’s go write the test.
Now let’s look at how to add this token to the header of any request that needs it.
So, guys, that is how you save yourself a little stress in Postman. If you found this article useful, please share. If you have any questions or find any mistakes, you can drop them in the comment section or email me so I can make adequate corrections. Thank you for reading.
Top comments (0)