Download as pdf
Download as pdf
You are on page 1of 9
sorn912a, 10:20AM Gating @ Bearer Token in Postman / Newman Automatically for @ Collection | by Tyler Owen | ITNEXT This member-only story is on us. Ungrade to access all of Medium. + Member-only story Getting a Bearer Token in Postman / Newman Automatically for a Collection eR, Tyler Owen - Follow ©I® pubishedin NEXT « 8minread + Jan‘, 2022 itpssitextolgettng-2-bearertoken-in-postman-newman-automatcaly-fr-a-collecior-4300%a0dete0a 198 sorn912a, 10:20AM Goting @ Bearer Token in Postman / Newman Automaticaly for @ Collection | by Tyler Owen | ITNEXT Our platform requires a Bearer Token for authentication on all APIs. During the normal flow of things we get this token from the Frontend UI; however I wanted to be able to perform some automation testing and this doesn’t involve the Frontend. The team had created an internal-only service that would allow me to programmatically get a Bearer Token with a username and password (and no 2FA). This is what the team used for a few other tests they would do and it fit the bill exactly. Open in app Ol & searen FAwit O @ Automating Postman nips text olgettng.2-bearertoker-in-postman-newman-automataly-fo-a-colector-d800ta0de1ea 29 sorn912a, 10:20AM Gotting @ Bearer Token in Postman / Newman Automaticaly for @ Collection | by Tyler Owen | ITNEXT Now that I had a way to get a Token I wanted to automate this so I could run the collections via Newman in GitLab CI/CD. For this to happen I needed Postman/Newman to be able to automatically acquire this token and then use it in all of its calls. This is exactly what “Pre-request Scripts” are for! So let's go through how to add this to a collection. In the Collection, not one of the Requests, you will need to setup 2 things. Authorization Tab In the Authorization Tab, set the Type to Bearer Token and for the value of the Token put your variable, I used {BEARERTOKEN}} Authorization @ Pre-reauest Sept @ Teste Variables @ This authorization method wil be used for every request inthis collection. You can overide this by specifying one in the request Type Bearer Token Leatn more about authorization » ticaly ganersted when you send the request. eneitve data, Voriabie. Leen more about variables » Token BEARERTOKEN) Pre-request Script Tab In the Pre-request Script Tab, this is where the magic happens. Here we will use Postman to make a call to our API with the correct params, parse the response and set a variable with the Bearer Token. ntps:tnextiolgeting-a-bearor- token n-postmar-newman-automatcaly-for--collection-J3001a0detea sorn912a, 10:20AM Gotting @ Bearer Token in Postman / Newman Automaticaly for @ Collection | by Tyler Owen | ITNEXT Of course you will need to modify to fit your needs, but below is what worked for me. I get a JSON response back from the API with the token in the access_token key. 1 pm.sendRequest ({ 2 url: pm.environment.get("url")+"/auth/vi/token", 3 method: "Post’, 4 header: 5 ‘Accept': ‘application/json", 6 Content-Type’: ‘application/x-wom-fora-urlencoded 7% 8 body: { ° node: ‘urlencoded' 1 urlencoded: [ a {key: ‘username’, value: pm.globals.get(“UNAME")}, 2 {key: "password', value: pm.globals.get("PASS")} 13 ] “wo sy 16 (eer, es) => { ” 11 Set BEARERTOKEN 18 pm. globals. set("BEARERTOKEN", res.json().access_token) 19 11 console. log(res.json()); 2 })s postman_pre-request hosted with @ by GitHub view raw Requests Setup Now that you have those two items setup, each of your Requests and folders need to be set to Inherit Authentication from Parent. This will use the Bearer Token from the Collection! ntps:tnextiolgeting-a-bearor- token n-postmar-newman-automatcaly-for--collection-J3001a0detea sorn912a, 10:20AM Gotting @ Bearer Token in Postman / Newman Automaticaly for @ Collection | by Tyler Owen | ITNEXT Params Authorization Headers (8) Booy | Type Inherit auth from... The authorization header will be automaticaly generated whi you wend the request Leatn more about authorization > Conclusion Now that you have automatic authorization for your Postman Collections you can run your collections via Postman or Newman completely automated. This was perfect for my use case as I wanted to run a full API test every time the team built and deployed a new container into our development or QA Kubernetes environment. Since we use GitLab I was able to use the Newman container and run the collections from there. I will be writing up that process shortly. Hope you found this useful, if so, give it a clap or share it out. Postman Api Automation DevOps Automation Testing ntps:tnextiolgeting-a-bearortokenn-postmar-newman-automatcaly-for--collection-J3001a0detea sorn912a, 10:20AM + 8min read nips text olgettng-2-bearertoker-in-postman-newman-automataly-fo-a-colector-d300ta0de1ea 6 sorn912a, 10:20AM ® Martin Heinz in NEXT You Don’t Need a Dedicated Cache Service—PostgreSQL as a Cache Why maintain a Redis or Memcached instances when you can just use PostgreSQL... +» Gminread - Oct3 22 Qr Choos GGotting @ Bearer Token in Postman / Newman Automaticaly for @ Collection | by Tyler Owen | TNEXT ® syter owen Ubiquiti Unifi and Elastic Stack Logging Having a background in Cybersecurity and specifically SIEM, | enjoy testing out different... + + 4minread + Jul6,2020 See all from Tyler Owen See all from ITNEXT. Recommended from Medium ntps:tnextiolgeting-a-bearortokenn-postmar-newman-automatcaly-for--collection-J3001a0detea 19 10/1079, 10:20AM @ Wengi Glantz in Better Programming RDS Hybrid Multitenancy How to implement hybrid multitenancy in one application with Spring Boot, Hibernate, + + Bminread - Apr13 Sys Qi [loo Lists General Coding Knowledge 20stories - 450 saves Productivity 290stories - 142saves TINE ] Prat) B AiiBaderedain Building the Backend for a Slack App with NextJS and Vercel After discussing the idea of our Slack App in the “Building a Slack App for 1-1 Monthly. 46min read - Jun 20 Gotting @ Bearer Token in Postman / Newman Automaticaly for @ Collection | by Tyler Owen | ITNEXT © Patrocios Lemoniatis Why Audit Logs? (Hibernate Envers) Lets start with the basics. What are Audit Logs? Tminread » Jul2 New.Reading_List ‘4stories - 151saves Natural Language Processing “2A stories 319 saves aA Kong @ Lionei Tchami a What API Gateway to choose: Kong, Gravitee, Tyk or HAProxy? 6minread - Junt ntps:tnextiolgeting-a-bearortokenn-postmar-newman-automatcaly-for--collection-J3001a0detea sorn912a, 10:20AM €5 man atnauda in System Weakness Is private information being leaked via your APIs? Use These Method... APIs (Application Programming Interfaces) have become standard practice in today's. 3min read - May 23 & Q oe ‘See more recommendations Gating @ Bearer Token in Postman / Newman Automatically for @ Collection | by Tyler Owen | ITNEXT & Zahwah Jame! Mastering API Documentation: A Step-by-Step Guide In the world of technical writing, particularly API documentation, you'll find a common. + - Tmintead - Sep 19 nips text olgettng.2-bearertoker-in-postman-newman-automataly-fo-a-colector-d800ta0de1ea 9

You might also like