How to use Postman and PowerShell to Query API data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i show you how to create api calls with postman and powershell [Music] hello everyone i'm travis welcome to my youtube channel this video is on postman not the one that delivers your amazon packages but the application before we get into that don't forget to subscribe like share and all that good stuff it helps my channel and i've turned watching the subscription count into a drinking game i have to be upfront with this video my background isn't infrastructure not development if you're like me starting out pulling data from web endpoints with a scripting language like powershell this video will help you get started if you're looking for a deep dive into postman you may not find it here but you're welcome to watch anyway an api accepts some type of input formatted as a url does something with it and then hands it back as output postman is a free application that allows you to build and test apis in this video i show you how to use postman to build urls that can be used in powershell to retrieve data although i use powershell as the scripting language in this video you can substitute that with any other scripting language of your choice using apis along with powershell greatly enhances the sources to pull data from sure you can get process information from a computer or query ad but what if you want to know the weather conditions at a remote data center or the drive time between two cities this is where apis can help the problem with apis especially for someone like me with little programming experience is that they can seem somewhat cryptic that's where postman comes in postman simplifies the process of building the urls that target the api endpoint the best way to review this is just to walk through some examples if you want to follow along start by going to postman.com and downloading and installing the program i'll include all other relevant links below i'm going to use two different sites that allow some level of free access for this demo openweather.org to pull weather information and oxforddictionary.com to pull word definitions they both handle api requests slightly different and both have free options available you need to sign up to access these apis these apis are free but not anonymous there has to be some authentication in place to prevent abuse this is done with an api key the api key is like a password to the api providing security and authentication for api access when you register you'll get an api key by email or by logging into the site save that you'll need it for later let's get started in the demo we're going to format our api request with postman and then use powershell to retrieve the data first with open weather and then with oxford dictionary and in case you're wondering openweather.org is a site that i was using to pull some weather related information oxforddictionary.com was just one that i thought of as an example i really didn't have any project to use that with but it turned out to be a good reference anyway let's get started here i am at postman i should point out the reason i'm doing this video is because i wanted to pull weather related information into powershell and view it in a display of some kind when i found this could be done with open weather and api calls i thought others may be interested in how i got this to work open weather has a lot of good information documented on their apis i'm going to hop over there now and i'll go to current weather data api docs the first thing i'll point out is they have some good examples here to pull from i'm going to copy this one this is an example of how to get current weather data from a city so i'll copy that and then i'm going to go back to postman and the first thing i'm going to do is just come up here and enter the url now notice it added a q here q is the city code and it added london now i'm not in london i want to change this to minneapolis so this is the example i'm going to hit send and you'll notice right away it says invalid api key now as i mentioned before an api key is like a password it lets the api endpoint know who's making the request i'm using the free tier of open weather it's free but not anonymous so i'm going to grab the api key and if you're following along with this you do have to sign up to get that key they'll send it by email i'm going to go back to the documentation and we'll see here this is a better example it shows the app id and then your key so i'm going to come back so that's app id and type that in here and notice what it did right away it came up here put an ampersand in and then the api id now if i enter in my actual id it fills that in so what i'm doing is using these query parameters i'm building out that api string so now i have my city and i have my api key i'm going to hit send and now we get the results i kind of glossed over this earlier but all the results are coming out into this display so uh when it threw the air we could read the error let's just do that again here i got a code 401 and the messages invalid api key so i knew right away what was going on this is one of the nice things about postman is you can interact with these api strings in a kind of a gui method it just makes it easier to see the results and understand what's going on i'm going to send that again and i will change these api keys of course once i get this published so i don't care that i'm showing it but here we're gonna see a couple things um if we go down to let's see weather there's a lot of codes here i mean we can tell that there's clouds and broken clouds and there's an icon id and there's a lot of things coming through but temp is 300.09 it's not 300 degrees so let's go back to the documentation and see what's happening there's a couple things i do want to actually add a state code but we'll come back to that if we go through um i did a find and search to actually locate that but if we go to temperature i think it's down on the bottom so here is the temperature is the default is kelvin so that's why it's 300 metric celsius imperial or fahrenheit and if we keep going open weather is really well documented not all api publishers document this well so here unit format so here it's giving an example units equals imperial i'm going to change my api call to imperial so let's go back there and add units so now it should come back as imperial and again it just attacked that on the end if i hit send 80 degrees so that is accurate so let's add the state code because not all cities are well known and if we go back to the documentation we go all the way back to the top of this and here it's just showing city comma state code and here you can see london comma uk so let's go back and add that in so now it says city not found and i'm not sure exactly if there's a problem in the documentation or a problem with the format i'm using but what i found i have to do is i'm going to add a value and then i'm just going to slide that up so there's no key but i am i do have the mn as the value and you can see here now it's minneapolis and equals mn so if i hit send now it's coming back correct my point with this is how easy postman makes it to troubleshoot and build out these queries so now that i have this i'm going to copy it i'm going to go over to powershell i'm just going to paste it in here in case i lose it so i'll create a variable called weather and equals invoke rest method give it the uri and this does have to be in quotes and hit enter so now if i go to weather i can get all my information so just like that i built out the api string now i can use powershell to grab it and then i can go in and do like um i can pull up individual values so if i wanted to just get the temperature that brings me to another point on this if i go to weather and i go to get method we can see it's coming back as a ps custom object i do want to do another test this time you're running invoke hyphen web request let's do weatherweb i'll just give it another value equals okay so now if i pull that information here you can see i get similar information the difference is if i pull up the data type i'll use gm or that's get hyphen member here i'll do that you can see this is a an html web response object so with the invoked rest method it gave back a ps custom object and with the invoke web request it came back as an html web response so i find the involve rest method is just easier to work with to pull the data but i just want to point out those are two different ways to pull back data and the results are a little bit different than to get back there was one other piece of information i just wanted to point out and this is specific to open weather if we come back to icon you can see it's 0 4d and i'll forget that so i'm going to just copy that if i come over here i search for weather icons and you can see here it shows the icons that come back and it's got a little link here that tells you how to pull the data i have another one open here but if i put that in actual image changed let's just see a different one let's do 10n there so you can see it's just changing the icon and the only reason i point that out is if i did want to put this into a dashboard or something i could pull this icon at the same time as getting the temp and the other information so that's the first example using open weather let's move on to oxford dictionary i'm going to open up a new project and we'll come over to the oxford dictionary api information here is the oxford dictionaries api documentation and this is a little bit different first i need the url so i'm going to take this portion of it and use that as my starting point they've got an example here but it's not for powershell or postman for that matter let's just start by building this out so we have the base which is https so it's an ssl call and it even includes the port api version entries so let's what do we need beyond that here it shows we need a language and then we need the word id let's go over here to make requests to the api we'll see if we can find this information so here's language codes and you can see we can go to supported language code let's find english so there's a couple of them i'm going to do en hyphen us if we go back remember that this isn't a key value pair this is part of the path so i'm going to come back to postman and enter en hyphen us i'll go back to the documentation the word id and that's just the word so i'm going to put in azure now if i hit send it says an authentication parameter missing so if you signed up for oxforddictionary.com you got a api key and if we scroll down we should see it needs a couple things it needs an app id and an app key let's type those in now i have those keys typed in i need to find the actual value let me see if i can find it over here i'm going to go to api credentials here's my application id and key and again i'll delete this before i make this public i'm going to just copy these values in notice it's putting the values up here in the url so i'll hit send i'm still missing the authentication parameter okay so what went wrong well if we go back and take a closer look the api id and key are part of a header value this is a little bit cryptic but um if you look here it's getting the url and it's also passing in some header information and that header is the key so here's the difference we go back to openweather.com the api id or that key is embedded within the url the problem with that is that's getting passed to the api endpoint and clear text and if you're security conscious that could be a problem now i'm not particularly too worried about openweather.com but as a good practice keeping these values in the clear text web request is probably not a good idea so if you notice postman has an option here for headers under headers i'm going to add the app id and app key we'll go back to parameters i'll just copy these values over and then i'm going to delete them so what i'm doing with postman is i'm not putting that as a parameter in the get request i'm passing that through as the headers headers are encrypted with ssl so if anybody's inspecting that traffic along the way they're not going to be able to find that api id and key so now let's hit send okay en hyphen us is saying is bad so this is good i can go up here and i think i know what the problem is it's case sensitive let's try it again there it is now i have a bunch of data return and json format that is all great that's exactly what we're looking for so now we can submit this url but the problem is the api id and key are not part of that string so how do we handle that with powershell here i have that header information so i'm just taking the api id and the api key i'm putting those key value pairs into the header variable and i also have a uri variable which is the url we created in postman i'm just going to grab all that and run it so it's in memory then i'm going to create a variable called azure i'll do invoke rest method add the uri and add the header so now it'll pass the uri and the header now if i get the output from azure there it is now i have the results but if i just wanted to pull the definition all i have to do is run this command it's kind of mining down to find what the actual definition is but there it is and if you need to find what that is you can either pull it up in powershell itself or you can just come back here and look at the layout this is just taking this json data and creating a ps custom object with it that is how you build an api request in postman and use that information in powershell i went over a pretty simple example with openweather then moved on to oxford dictionary that required an authentication header so i hope this helps you understand better how to use postman to build these api requests and then use those requests in powershell i hope you found this helpful please don't forget to subscribe like and click the bell icons for notification when content is added thanks for watching
Info
Channel: Travis Roberts
Views: 4,754
Rating: 5 out of 5
Keywords: Postman, API, PowerShell, Headers, SSL, URL, web, invoke-webrequest, invoke-restmethod, devops, post request powershell, get request powershell, powershell API, powershell REST, rest api with powershell, web request powershell, powershell url, postman api testing, postman api, how to
Id: WqVD4h8OxS0
Channel Id: undefined
Length: 19min 40sec (1180 seconds)
Published: Sun Jul 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.