PowerShell Quick Tips : Invoke-RestMethod vs Invoke-WebRequest (Calling Rest APIs)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this Powershell quick tip in today's quick tip we're going to be taking a look at two commandlets we're going to be taking a look at the invoke rest method and the invoke web request now both of these commands are used to as you probably assumed it called apis um either rest apis or just web pages they can get you the plain html text as well so that could be very useful as well but we're going to be looking at them more on the side of rest apis now it's very rare that you would have two commandlets that do pretty much the same thing and both of these will you can invoke rest apis but they actually give you two completely different results one will give you a very simple results that could be used right away whereas the other one gives you a lot of extra data which could be very very useful but you do have a little bit more work to actually process the actual results of the API call so let's actually go ahead and let's first use a API route that is completely free to use so I went out on the internet to find this API and actually this API is pretty helpful um because it could actually give you um basically random user data so it'll give you like a name a gender an email an address all sorts of info basically for a random user that you can actually use to build up um like a data set um which could be very helpful especially if you're going through some of my other videos uh like the active directory management and you need some random data you don't have any data and you don't want to use uh the CSV generator or you just don't want to create it yourself you could easily use this API to actually generate some user data for you so that's actually pretty cool so here's the URI I'm actually going to provide this to you guys in the description down below so you guys can follow along with the video as well and that's why I'm using just like a free API not something that's like on my end um that's or something that's behind a paywall I want everyone to be able to actually benefit from this video so let's first look at the first one here and that's going to be the invoke rest method so let's go ahead and let's actually just call the commandlet so let's do an invoke breast method here and that's going to take uh basically two parameters in this case the URI which we have in our variable here and then it also takes a method which for this API call we're going to be using the get method so if we actually go ahead and we run this here we actually see that we get a result and if we actually store this in a variable called rest method here so let's go ahead let's run this once again and if we do a dollar assign rest method and we do a DOT to see what we have we actually have a dot info and we have a DOT result so if we do the dot result in this case we will see this is the info for our user the API returns it very nicely we've got the gender we've got the name so we've got the title first and last name we've got the location we've got email so this API provides you tons of info you can access it through results and you can also do a dot info and this will give you a little bit of information on the seed of how they created that user the results in the version of the API so this is a little bit less necessary this is something that the API provides you um but that's pretty much it you don't really it's just another piece of data that they give but the most important part to notice is that we're getting back Powershell objects oh we're not getting back Json which you would typically expect back from a rest API we are getting a Powershell object and that is because in the invoke rest method it actually calls the invoke web request and already pre-processes it it does the conversion from Json into a Powershell object and only gives you the results it doesn't give you the status code it doesn't give you any of the headers it doesn't give you the raw content of the page it really just gives you that result converted from Json into a Powershell object so now let's take a look at the other commandlet which is actually the invoke Rebel Quest and what we're going to do actually is we're going to go ahead we're going to store that right away in a variable here called Web request and we're going to do an invoke Dash web requests and once again the parameters are actually identical so we're going to pass in our URI and our method once again is going to be the get method and let's go ahead and let's run this line so if we actually take a look at what is inside web request we will actually see that we have tons of information here so we have the status code we have the headers images raw content length now the only thing is actually now that I see it so here it is so here's actually all the details here unfiltered so here's the the status code the status description so we know that it returned a status code of 200 it's okay here's the content so here's actually the result and we're going to take a look at how this looks like in just a few seconds then we have the raw content we have the headers that it returned uh the images input Fields links raw content so this is all stuff that if you were to use um like an internet browser and you go to the web page and the response this is the type of response that your browser would get it would treat it now you wouldn't necessarily see all this info you would see the content on your screen but it would also return headers that you would be able to go look in your browser in the inspector and see those headers and see all the status code would either be 200 201 or in the 400 series if something goes wrong or even 500. um so just let know that the web request will actually return the full response of that route whereas the rest method will only return the actual content the actual result of the call converted into a Powershell object now what we could do with the web request so if we do our web requests and we go look at the content we are going to see that the content is basically just a bunch of text and this is actually in Json so what we would actually have to do here is we would have to do a convert from Dash Json and actually run this here and there we would actually get our Powershell objects so if we actually do this here so if we do this we store this into results now if we go and look at see what result gives us here we do a DOT we have our dot info and we have our DOT result so what the invoke rest method really does is it calls this invoke web request and then runs this and then returns you that result uh so you're losing the headers you're losing that status code which maybe in your script you don't necessarily need the status code but if you do want that extra data because you're writing a commandlet or you're writing a function and you want to make sure that that API call succeeded maybe you just want to check that status code to make sure that you got a 200 status code if you didn't get a 200 status code then you could throw a custom error to the user to say that something went wrong so that's the benefit of using the invoke web request compared to invoke rest method um there is nothing really wrong in my opinion to just use invoke rest method if you know the API very well uh it could serve you very very well there are people that will always recommend to use invoke web requests just this way you have that extra data it doesn't hurt to have that extra data it doesn't hurt to have the headers it doesn't hurt to have the status code it could give you some better error detection it could just make your script a lot a lot better A lot more secure as well just because you can actually look at the status code prevent errors make it look nice to the user so that is really the difference here um is one will give you the actual response and one will give you a Powershell object um to where you you don't have to do anything it's usable right away whereas the other one requires a little bit more extra work to get to the same result as invoke rest method so that is the two main differences between uh invoke rest method and invoke web request I hope this kind of clears things up for you guys these are the two main ways to call apis they're both completely valid they will both most likely work in any situation in volkrest method I've never had an issue with it there might be situations where the API maybe isn't written at a hundred percent to where you might need to call the invoke web request instead but I have yet to encounter that myself but these are the two ways I would definitely pick one and once again kind of like all the other things that I've mentioned is that if you pick one just kind of stay with that one make sure that you know on line four of your script if you're using invoke rest method on line 20 use invoke rest method as well like don't kind of switch up halfway through so that'll just confuse people that go to read your script and wondering why you're doing all this extra conversion whereas above you didn't do that conversion so that's pretty much it if you guys have any questions or comments for me please let me know Down Below in the comment section I know that I've had this question quite a few times so if you guys want to know a little bit more uh just let me know what the question is down below and I will do my best to answer you guys also be sure to hit that subscribe button hit that like button as well make sure to hit that notification Bell to be notified when that next video comes out and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 13,226
Rating: undefined out of 5
Keywords: powershell basics, powershell, windows powershell, programming, scripting, powershell scripting, powershell scripting tutorial, powershell tutorial, how to, powershell api responses, powershell automation, powershell beginners, powershell variables, quick tips powershell, quick tips, learn powershell, powershell for beginners, powershell commands for beginners, powershell api access, rest api, invoke-restmethod, invoke-webrequest, invoke-webrequest vs invoke-restmethod, api
Id: duZk0GbMmPo
Channel Id: undefined
Length: 11min 16sec (676 seconds)
Published: Thu Nov 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.