Calling Web API from C# Simple Console Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome back to joyful id consultant youtube channel in this video what we are going to do we are going to consume azure web api application in using simple console application okay so i am going to make use of the web api that was published to azure website azure app service and we are going to consume that azure app service uh in simple console application okay let me show you what azure application i'm talking about so for that i'm going to go to my azure portal and okay so i'm talking about we have azure web api test application yeah so this is up and running this is base url so if you remember my last video so at this url it's going to return me a message okay not at this one and there should be message you should get me all right so yeah i am making use of this web api so basically we will write simple client-side code to access this web api and see this message in our console application okay so that's what we are going to do to do so i am going to launch my visual studio and i am going to create a new project which is basically a console application so console app and i would name it as test my azure web api that's it it's getting created all right so what i am going to do here i am not going to write too much of code i will really simplify that so that we will write a code to access that web api and uh see that message here okay all right so obviously because web api is uh can be accessed using http so obviously the one we have to uh the object we need to create is for http client so this is present under http user using systemnet http okay all right here we go and i would say client equal to new http client okay and i can also okay i can do that later all right anyway now what i need to do is client dot specify that all right sorry client dot get a thing i am directly passing this url so which url this guy and but here if you'll see it is going to return us one http response okay so what i'm going to wear response task because if you see that is returning a task because it is uh returning asynchronously so i would create response task now now what i will do and our response task dot so i will wait for that call to be finished now what i am going to do i will check if that response task dot is completed then i am going to put result equal to response task dot i am going to once this task is completed i am going to read the result from responsetask.result all right now this is not enough because if you see this is still http response message we have to get the actual message so to do so uh i'm going to make another check if result dot is success code see this is success status code that means if the call has been successful then only i will confidently look for the message so what i will do what i'm going to do is the result i would say message okay equal to result dot content dot read as string asynchronous because i know that uh this guy this web api is returning one string message so now i will have to wait for what message which is a task wait now what i'm going to do is console dot message from web api is message okay so if you see this is returning string task okay i'm going to finish it up just to make sure uh i am just going through if i missed anything all right so if everything is good you should call that web api and return the message to our console let's see probably it is successfully making a call but i think there is some issue oh no it would have worked but let's do one thing let me put console.read so that we will get to see the result um so what it says is this code is not yet finished so we have to the final message would be this is still is see the problem is the message though we named it as message actually it is a task okay and i would name it as task install in this task dot result so the result is the string final message now we should get it also i'm going to make i'm going to write some console writeline so that we will have better understanding so i would say writeline calling web service sorry web api ok calling web api that's it so it will give us some idea what's going on all right let's see this should this time you should get us the actual message wow see happy independence day is coming from web api so so that is it so this is the simplest way to test your web api without any uh you know you know without any formalities or fine tuning like making it asynchronously but in real time we will not code this way to consume any web api but we will be writing an asynchronous programming so in next video we will see how to asynchronously call this make a call to this web api using uh we will we will show that using a simple win forms application okay that's where we can visually see what is asynchronous thing that that i am talking about okay all right so that is it friends uh so just to rewind what we learned what we saw is so we have web api hosted in azure app services and we we wrote a simple console application and we are successfully able to make a call to that web api and show that in our console the result that's it so hope you learned something and if you like this video please click that like button and don't forget to subscribe and see in the next video bye
Info
Channel: Joyful IT Consultant
Views: 18,466
Rating: undefined out of 5
Keywords:
Id: SUtXrn1s8OQ
Channel Id: undefined
Length: 12min 31sec (751 seconds)
Published: Mon Aug 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.