Step by Step Tutorial - C# REST Client

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay in this video we're going to be learning about how we can consume a rest service from C so taking an agile approach we've got a user story format here um as a developer that's me or you um I want to call a restful API from my c application that's say what and why are we doing it so that we can consume the resources uh it provides okay and the ingredients well um Windows PC to run visual studio uh Visual Studio obviously internet access possibly not mandatory and then 30 to 40 minutes of your time now you could be pedantic and say you don't actually need a Windows PC to Run net code but uh for the purposes of this video we will be using such a PC right so I'm just going to show you the the web service the restful web service service that I've put on uh online for you to use as part of this tutorial if you go to the blog it has the uh URL there for you to use now there's um two um potential actions that we can work with index and show there are others create update and destroy that I disabled for the purposes of this video we're just going to consume it uh and you can see here we're returning a Jason string Jason is Javascript object notation uh this video won't go into too much detail about it you're probably familiar with what it is if you're if you're looking at this video it's basically just a structured way to return data effectively I'm just showing you here that um the web service can actually return HTML as well but we will just be working with the Jason now I'm just copying the output uh from the web service into this uh online tool it's really good Jason editor online just paste your Jason in Click the button here and that kind of formats it for you it will tell you if there's a regular ities with it and all that kind of stuff is pretty cool and then you can put it over to the right hand plane where it gives you a nice hierarchical view now I find this Tool uh really useful when I'm consuming Jason so what what you can see on the screen here is basically the uh the client mocked up in a tool called balsamic although there's a lot of really great tools out there that you can use to do something similar uh and you can see there's there's two um text boxes one where we're going to put the URL of our web service that you saw a bit earlier on we're just going to put that in there there's another text box which is basically going to show us the return from the web service now we you know again it's fairly basic um very basic user interface but again mocking up just helps me think about how it's going to flow and how it's going to work and you can just see the the two steps of once you click the button the Go Button the the data is returned and here's what it will look like in Visual Studio when you actually uh come to creating the project project uh you can probably just skip straight to that stage wi framing is probably a bit over the top but again I've just included it uh as part of the process all right so let's get on and uh start creating our new project as you can see that I'm using visual studio Community Edition 2015 uh now I've found this to be a great tool um with with no real um I've not come across anything yet that that I need that is not in this version of the tool it's really good so we are going to um visual C uh sharp Windows Form application project just give it a name um we'll call it something like rest client although I'm from memory I think I've already got a project called that so let's just uh call it something like um yeah just give it a slightly different name c rest client just to avoid any duplication we'll just wait for the project to set itself up I'm actually uh running this is actually a Windows Virtual Machine running on an Apple Mac my goodness um I just do all the video editing on a Mac and it just makes a bit more sense to do the screen recording on a Mac anyway that's a bit of a digression so here's here's your um Windows Form you're probably pretty familiar with this um so we're just going to um get the environment set up uh just change the form heading you can probably fast forward a we bit if if this is all just kind of uh really boring to you um and then what we're basically going to do is we're going to take our mocks or yeah basically take our wireframes not our mocks there's a difference I've recently sort of found out we'll take our wireframes make reference to them and we'll just basically start building up our form so uh there's a text box for the URL um for our web service that uh you will be calling um I I again this is just kind of hygiene type stuff I like to give my text boxes of meaningful names you probably have your own methodology I use I usually prefix mine with txt using kind of camel case Okay and then we're just going to add our second text box uh that we'll use for our response back from the rest full API and we're going to make this we'll give it a name first text response probably a good name is any and then we're going to make it multi-line because the responses potentially could be quite big so multi-line equals true and then we'll just set some scroll bars we'll just set the set the vertical ones so we can scroll up and down uh pull that down a bit and then what we'll probably do is Anchor it to the form so when yeah I mean again you probably know all this stuff um probably if you're looking at this video you know all this already um but yeah that that just basically anchors it to the form so when we resize the window you can uh see what's there well that a button to It Go exclamation mark very exciting and again I'll just rename that um again I I prefix all my buttons with CMD which is a command I don't know where I picked it up from possibly Visual Basic days or something like that it's kind of funny how that kind of thing sticks with you isn't it uh I'm going put a couple of labels on just to to keep it um in tune or in line with our wireframe I I very rarely if ever give my labels names unless of course they're going to change but if they're static and they don't change then I never actually uh rename them I don't know if that's good or bad it's not never really caused me any problems so I'll probably just stick with that approach all right so our UI our very complex UI is basically done uh so now what we're going to do is we're going to just add another class to our project and this class is actually going to be our rest client class that you can you'll be able to reuse again in other projects um a little bit further in the video so yeah just pick class give it a name rest client probably a little bit further along in the video when we come to populating the Constructor and and the the single method of the class you'll see a little class diagram that I've prepared for that so there you go rest client um class ready to go now um this this is just a bit of an asid again you can probably fast forward a bit um again of course double clicking on your command button will create the event handler the command click event handler for your button and you can see it down there as well again you probably know all this stuff what what I'm just going to do here um first I like to put some regions in even for really small projects I like regions they basically are just kind of uh areas of code that you can group together and then you can kind of um expand and collapse them it just keeps the code uh for for my purposes anyway it just makes it a bit more readable you can kind of get rid of stuff that aren't used that much so there's our UI event handler region and so yeah what I'm just going to do here again you can probably fast forward if you're not too interested I have a a function that I reuse all the time and basically all it does is it just writes out strings to a text box a multi-line text box here it is called debug output it takes a string as some input uh now it writes out the input string to your output window in visual studio and yeah output window there and it will also uh write out to our text response box uh here um and it will scroll down the more stuff you put in it um and I just find it a really useful function yes visual studios's got some fantastic in buil debugging features but I find this is just a really quick way to kind of help me understand what's happening in the code so feel free to use that we just we'll just test it out we'll put something in here and you'll see it's probably easier to show you an example of what I mean by what it's going to do it's very basic actually but I just find I use it all the time especially when I'm starting off with smaller projects and we will actually use it when we get our Json response back from our rest API web servers so let's just give it a try get rid of these windows here get our output window ready and then we'll just click the Go Button don't know what I did there and then uh you'll see what it does okay yep so it just writes out a string and Scrolls down the text box the output window Scrolls automatically and that's it okay as promised here's a couple of diagrams for you first a sequence diagram very simple couldn't really get much more simple our RIS client C on the left they making a HTTP get request to our users resource or restful web service and getting a adjacent response back the when effect that's all this application is doing is making a HTTP request sorry to ruin the magic for you and uh here's our class diagram our rest client class with two methods a Constructor are very simple and a make request method which is where everything is basically going to happen and we have a smaller numeration that lists the verbs that our class could potentially use we're only using get in this instance okay back to our rest CL class and I'm just kind of tidying up the the headers here um we need four system iio net and I thought we needed text we don't actually need that um so that's actually somewhat redundant you'll see in a minute it's quite cool this version of Visual Studio it kind of color codes uh the using statements depending on whether they're used or not uh just paste in our uh enumeration our list of HTTP verbs again we're only actually using get in this uh tutorial so it is a bit redundant okay so just before we start coding a little bit about the HTTP web request Class B my mouthful um now basically in herit as you can see there from the web request class um and we actually use the web request class Constructor uh to create a HTTP web request class my goodness it's quite a mouthful now if you actually search for this documentation online it will take you to the right page I just search for C HTTP web request and if you just read the remarks uh it basically just tells you um how you should use it and why you should use the web request Constructor so on and so forth and it also details um the get response uh method there I'm just highlighting and how you use that and then how you get a response stream and then you read that um yeah so it explains it better than I can um all the documentation is there so now if we jump over to the code everything should become clear so originally I was just going to paste all the code in um and I actually made a video where I did that I just pasted the code in from a notepad that I have to the the left of the screen um but I thought that's totally pointless you've come here to see and have an explanation on how all this stuff works so just pasting code in is pretty much redundant so you're going to have to put with my slow typing um but I feel it's more valuable uh that we do it this way so the first thing we're going to do is create our two uh properties class properties uh the Endo that's just going to hold hold our URI and then the second property we're going to uh have is the HTTP verb uh that we created above the numeration um now again for the purposes of this demo so a little bit over the top a little bit redundant but maybe going forward just place that going forward it's Pro probably a good thing to have if you expand upon the class right so um what we're going to do now yeah we're going to actually do the Constructor so rest client here we go and all we're going to do is just initiate or instantiate the two properties going to set the the end point just an empty string we'll set that later when we actually create an object and yes we're just going to set the the verb to get by default as that is all we will be doing as part of this demo and then finally um the moment you've been waiting for we are just going to uh make the make request method and this is where uh all the magic happens we're going to return back a string I just like to uh of create the string right at the start and then just kind of return it back uh at the end and then just do a quick build to make sure everything is sort of wired up at a very high level just make an empty string we just return it back we go build yep succeeded so everything's syntax there there a great thing about Visual Studio is you know immediately if you do have something wrong it's it's fantastic I mean I did a bit of rails programming a while back and uh you get none of this kind of stuff on not that I could find anyway anyway so we're going to make as as I said just in the intro a HTTP web request uh object we'll call it request and I'll just show you um trying to uh create uh this is the Redundant Constructor create a web request just show you the methods here just show yeah new HTTP web request create and then we will just put in the endpoint now that shouldn't work because you can see it's actually it's been as it said in the documentation it's been uh what's the word I can't remember the word discontinued it's not discontinued um retired made redundant so as it says in the documentation basically what we have to do is we have to affect L cast it using the web request object and that should work now now I did actually try just using the web request object and it actually does work so I think the the HTTP request object as it says in the documentation just provides a HTT specific implementation of this so it's probably best to use that okay so um as part of the uh setting up uh we're just setting the Quest objects method to get again this is where our verb um en numeration comes in up above it's a bit over the top but we're just setting it there okay so what we're attempting to do now is get the response back HTTP web response and that's using the uh get response method which is a synchronous request to the server there we go get response okay so that just passes back a response it's not actually passing back the the data it's just basically getting a was it successful was it not successful and if it's successful then we will uh get a response stream which is basically the stuff that we want to uh return back okay cool builders okay and we're passing back HTTP web response object so just to status code check to see uh what the synchronous response back from the server was if it isn't okay and you got a list of options there in the status code then we'll just um we'll just throw an error or throw a through an application exception okay let just give a a horribly generic uh response back something that request I don't know error code I mean if you know the error codes is probably relatively meaningful but um we just put that in tell to string there we go okay so basically what we've done so far we have created a request object which is a HTTP web request um we're then um making a get response request method um passing back HTTP response object and we're checking the status and now we're going to assuming it all went well process the response stream and I say it could be Jason could be xil could be HTML I mean I'm expecting Jason and tutorial will go into pausing the Json and using it but it could be anything and you'll see that when we actually run it okay so we're just setting up a stream object and we're calling get response stream and this is actually bringing back stream of data and putting it into the response stream so we'll yeah I just put this in because I I start to lose the plot a bit when I'm you know using these using statements and same with f El's and all that kind of stuff I just like to kind of DeMark which one is which it helps me um keep my sanity do a bit of housekeeping okay so we're expecting a stream of data but probably just check to see if there's anything there so if a response stream isn't null May we've got some kind of data to pass back then what we need to actually do is create a stream reader to read the response stream okay new stream reader yeah and pass it in the response stream stream so as you can see it's a little bit convoluted but it does make sense if you follow the flow of the code it's multiple steps and setting up basically just making a HTTP web request that is effectively and then getting the data back and reading the data um but it can trip you up if you've um you know not done it before and this is why I do these videos so here's a string that we're going to pass back we're just going to basically um using the the stream reader we're just going to read to the end of the stream and then we will eventually pass that string back okay so let's build that it's all looking pretty good I don't think I've made any mistakes yep I think that's I think that's looking pretty good and just make sure this is indented correctly y all right okay that's looking good so we go back to our uh form code I've cleared out the click event we're just going to now create an object a r client object call it our client and we go Constructor doesn't take any uh parameters so we have to set them afterwards as we're doing now we'll just set the endpoint to whatever is in our text box okay using our debug output function just say uh you know M clients created excuse me my voice is going and now now the the magic is happening just create respon string I'm probably doing this a bit long handed but I like to just kind of set it up with an empty string and then I'll uh set it to the you know the make request response and then we'll just again using debug output we'll just see what we get back on the screen or in our uh our in our application all right Bill okay so here here's our uh test web service or rest uh rest API and we'll just uh just make sure it's still all working so we just want to return all the users from the users resource call it the index method this this application is actually written on rubyan Rails which I was talking about before it's pretty good but um it's good for rapid prototyping so we'll just anyway digression we'll just paste in the URL and there we go we're getting back the Json response using our rest client class and again we can even just go in here and you can change this and reuse it they can return back a specific resource with an ID of two and again just to prove a point we can just doesn't have to be Jason it can be HTML it just gets a stream of data from a HTTP web request okay that's about it we're done um as I said the the msdn resource on the HTTP web request uh class is really useful I suggest you read that also uh my blog up there has basically step-by-step instructions um as shown in this video um so please go there take a look leave a comment any suggestions for improvement um are more than welcome songs are constructive um next time what we're going to do actually is work with the Jon um response that we can get back from any restful web service and work with that der serialize it and and pause it and do all sorts of stuff with that it's a very uh very useful skill to have these days so that's that will be next time it will be on the blog and it'll be uh on the YouTube channel as well so um yep the web dress is being gone and I'm also on well YouTube obviously and Twitter um so again thanks for watching and I'll see you again next time
Info
Channel: Les Jackson
Views: 288,283
Rating: undefined out of 5
Keywords: c#, Step by step, turorial, rest, rest api, rest web service, binarythistle, .net, json
Id: 11f5KzVNQ90
Channel Id: undefined
Length: 24min 27sec (1467 seconds)
Published: Tue Oct 11 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.