The new autocomplete menu contains a list of all the variables in the current environment, followed by global variables, making your API environment setups more accessible from the Postman interface and introducing a pretty significant time saver once you have your environments setup properly. More examples and suggestions can be found on the excellent docs of Postman.The Postman team has been hard at work lately releasing their API data editor and introducing variable highlighting and tooltips. The configuration we just created can be saved and shared to other devices or to other colleagues that are working on the same project. This simple workflow is just the tip of the iceberg of the many advantages of Postman variables and environments. Once done we are ready to send a request to an endpoint that requires authentication, such as /users and see the results. Paste our variable in the one field available. Click on “Authorization” and select “Bearer token” under the Type dropdown. To do so we just need to open a new tab in Postman, paste the full URL of one of our authenticated endpoints, such as /users and, in the request headers make use of our newly set variable. This is great, as now we’ll be able to make request without manually pasting the token. We can confirm it works by running the request again and then clicking on the top right eye icon (Quick look environment variables). This easy 2 liner gets the response and sets it to our env variable. json () // set Authorization to the jwt token pm. To do so, select the “Tests” tab and type this: We now want to extract this token from the response and assign it to our other env variable, Authorization. This is of course the least secure of the APIs since we’re not even passing an email password combination, but that’s ok for a test server! Once the url is typed we send the request and sure enough, we have our token back. Postman will confirm URL is defined while we type via and autocomplete popup. This means that localhost:3000/auth will become /auth. We want to hit localhost:3000/auth, but instead of hardcoding the host we will use the environment variable we just created, by replacing localhost:3000 with ``. Save the values and select the newly created environment from the “Environments” dropdown on the top right of the UI.Ĭreate a new request by typing the endpoint we want to hit into the main input field. With this setup we can start hitting the auth endpoint and make use of our first variable in Postman. In the picture I added an initial value of localhost:3000 for URL, since I am running my server locally at first, while I left the auth field blank. Open Postman and click on the cog on the top right section of the UI.Ĭhoose a unique name for your project, and fill in 2 variables, URL and Authorization. If you want to follow along and try all the steps below you can clone this repo, follow the instructions in the readme to install and test every command against this test api. Let’s see how we can avoid this manual setup thanks to Postman.Īs you will see, environments are a very powerful feature to stay organised in Postman and to make our workflow much faster and portable. Moreover, every time our token expires, we will have to repeat the process of hitting the auth endpoint, and copying our new token. While this works, it becomes tedious quickly as we will need to paste the token in every new tab we create on Postman. We would then be able to paste this token as a header for future requests, such as the above example to request the list of users. The easiest way to do that is to first hit our authentication endpoint (for instance passing a username and password, inspect the response on the Postman UI and manually copy this token. To accomplish this a client may expect to pass a token as part of the request, say as an entry in the headers in the form of a JWT token. The server expects the user that performs the call to be authenticated to make this call. Say, for example, that we are hitting to retrieve a list of users that are registered on our platform. One of the most common cases is having to hit an endpoint that requires some sort of authentication. This will make it easier to understand why and when environments are useful. Life without Postman environmentsīefore explaining what an environment is in the context of Postman, I’d like to list a couple of the most common scenarios I face when working with an API. One of my favourite aspects of working with Postman is its environments feature. At the same time, devoting a little effort in learning some of its best features can quickly boost our productivity and development speed. Postman is one my favourite tools at work to test APIs.Įven at a simple level Postman easily becomes invaluable.
0 Comments
Leave a Reply. |