Creating A RESTful Web Client With Unity Engine Networking API - HTTP GET, POST, PUT, and DELETE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the guys are going system ready and welcome back to my channel so first of all thank you again for joining today I really appreciate your time now so when a class QP having subscribe to the channel to please do so we're just clicking the button below it is subscribe it it's really gonna help me in bringing you more videos very excited because I'm gonna walk you through creating some restful plane and this restful plan is gonna allow us to call web services rather web and then basically consume the response for those services in our unity application so I'm going to be walking you through the example that I created I'm gonna show you some of the core pieces that I created for you and also made available via github so what I'm gonna do is jump into unity can I start working on it thank you guys alright guys so let me show you what I build for you guys to work with unity and for those of you who haven't have experience with creating a rest flying in actually calling Web Services and going to be creating a web services in this video it's going to be a very simple web service but it's going to basically allow us to do simple HTTP methods such as doing an HTTP GET do an HTTP POST or delete and then also output so what I'm gonna be doing is basically creating something very simple that we can call from within unity and then I can show you some of the more advanced options these client libraries that I created that it's gonna be available it actually is actually available in PA github right now you can download it you can do anything you like and it's basically offer for you to be able to use it in your projects I'm gonna be adding more features as I get more comments from you guys in any suggestions you know I'm more than welcome to to make changes in editions if you need to so let me go ahead and walk you through what I have right now and then once I show you some of the implementation we can jump into an actual example so right now this project is the one that I submitted to github and get help there is a unity rest client and you can see that the entire project is available there you can download it from github comm /er ER v /u nadir s client and i started it yesterday so you can see that that was my ladies commit so there's not a lot of functionality in it but there's some common HTTP methods that you can use to call right now I can I'm gonna be adding more things such as security and and also you know maybe more methods as we you know as we get more into it okay so what do I have right now what am I looking at so right now I'm looking at a rest client example and this Rick rest client example is basically making a call to the azure of vision API so as you can see I have you know basically a poco and a poco is basically a playing a plane object and these basically allows me to pass in two of the parameters that I'm exposed in through the inspector I'm exposing the client ID and I'm also exposing a secret I'm also exposing a base URL so if I go back to unity and we look at the inspector you can see that some of those variables are exposed such as like I say base URL the client ID and secret and and this is used the requirements to call the azure vision API they require that you passing the client ID you passing the secret and then the way that this works is you give it a URL in that URL is the image that we're gonna be doing OCR and all CR means that we're gonna be doing optical optical character recognition meaning that we're gonna be struct in data and data from an image so I sure has that capability they have a lot of different web services available this was just one that I pick but instead of using theirs I want to build our own and actually you know show you how to build your own and also call call your own so for now we can just you know run this example and again show you how it works the the other piece now that I built the request header they also build the image URL is that we need to actually call in actually create a to start core routine so the reason why I'm doing this because this is some of the you know requirements for the unity implementation for calling web services they require that you there they do what's called a sync operation and within a sync operation you have to check when the a synchronous call is completed when it's completed we get a result back so I'll show you that code and explain that to you because I think it'll mean you don't make more sense as I show you the curve versus explaining all the theory so the way that it works in the way that I build it is a creditor was a rest web client and you can see this one right here and I wanted to make it as simple as it was gonna be to just make a call I didn't want to have to have all these configuration and all these crazy stuff so it's actually pretty easy to call it you i implemented a singleton pattern as well which is also included in this package and the way that it works is you just basically call the web basically when i'm on the class which is res web client then you tell her you want a single term which is going to be the instance and then you tell it what method you want to call so if I were to do something like this and say that I want to implement this in my own project all you really have to do is just incorporate the rest client folder that I'm giving you and then you say instance and then you have different methods in here if you type in HTTP you can say ok when I call it lead I want to call again I want to call a post in a pool depending on what you need if you need to delete a record from a web service this is the one that you will call and then you can see the parameters are shown on the right hand side v for instance the delete takes a URL which is the URL that we're putting on the top or you can just pass in the URL the other thing that it takes is a callback because the reason why I wanted to do a callback is because I want to know okay when when the delete finishes I want to get a callback to and a specific method in this case and just clear in an original request complete and passing they called callback object that we're getting back and then basically explains some of the some of the different properties I included in the clang which one of them is a status code the data that we're getting back in this case with what I'm really get any data because we are just deleting but we will get an a status code and we're also might get an error so that's why I always make sure that those are coming back well in the case that we're only doing Italy we're just gonna get the status code data might not be populated in there might not be purple later if you're not getting an error but if you do get in there you will get you know I feel with that information but I'll show you that in the code but just know that these are the metals that are available right now and like I said I'm gonna be adding more okay so in this in this case I needed to pass in the base URL which is gonna be the URL and I'm passing in here so that's the first parameter then you do a comma and then the next parameter in this case because I'm doing a post and doing a post is normally doing an insertion to a web service so if you're creating let's say you're creating a player you're creating an order you're creating an invoice those type of things are normally done in a post if you're doing a pull that's normally taught something where you're gonna be at doing an update if you're doing a delete of course it's gonna be deleting something a resource as well that's what rest calls them or if you're doing a get is normally because you're gonna get data so the first parameter is gonna be the URL that we need to call the next parameter it's gonna be the actual data that we need to post so we're gonna be posting an option in this case so I want to post an object that is that is type that is actually an object type and then in this case is gonna be a JSON object so I'm passing in this entire this entire object which is converted to Jaso but you see in the JSON utility that unitive provides and then this code here just don't if you haven't deal with deal with delegates don't just it's real actually really simple all this is doing is you're creating an action the this can actually be called anything we want I can call these requests I could call I can call these we go back I call these the response I just call it our because it's gonna be the object that I'm getting back from the con request complete so the first argument it's gonna be the response that I want this is using a lambda so I can call these whatever I want but then just know that this is gonna be the metal that is gonna get called once these HTTP POST finishes and then we're gonna basically pass in the response which is going to be passing here and then the last parameter is going to be headers and if you notice we're looking here we do a comma this is nullable so meaning that this is not required if I didn't want to passing headers I could actually do this and then end it with a semicolon and that should actually work I'm missing one parenthesis so I made the last parameter optional because you know you don't have to pass in a header every time in this case the these web services specification requires that you're passing the authentication which in this case is a client ID in a secret but if you just have something that is helping up it's open and you don't need security on it you really don't need to passing anything there but in a lot of cases you might need to pass in something like language you might need to pass in you know what the content type is gonna be so there's a lot of different things that you might need to pass in the headers in order for the web service or response to you in the appropriate manner based on the specification of the person that is creating the web service or the you know the company that is giving you the specs so that's how this works let me show you the implementation right now so I'm gonna go and show you the rest web client so this is actually fairly simple there's not a lot in here and and I might say simple because I've been dealing with web services for a long time but one of the things to keep in mind here is like I said I didn't want to have to you know create a monobehaviour have to deal with that and I ended up creating a singleton class and if we go into a singleton class what what it is actually doing is just creating a scene that one instance a static instance of the class which is gonna be the generic type that I'm passing that I'm passing in so that a generic terms and I'm passing in in this case is gonna be the rest web client and then I'm just basically telling it to create a static variable and then exposing a property which is the instance of the type generic that I'm passing in I'm gonna go through Gina harrassing more you know in more depth in one of the future videos so don't get overwhelmed if I'm going over these so just keep in mind that if I'm creating a single tone this instance this variable is gonna be available for you and there's gonna be an instance that is created in you know in the wrong time in the pipeline of the game that it's just gonna be a singleton instance so here's none of that what is gonna do so let's go back into here so the way that is work the way that this works is gonna be so no we're creating a class of type rest web plan it's gonna be called whatever you want in this case I'm creating a restful plan so I needed to make it you know give it a name that was meaningful and then I'm just calling the singleton which is the one that I just show you here and then it takes a generic so I tell it what the generic is gonna be which is gonna be you know the type of rest were plying so by doing this I know that by calling rescue up client I don't have to create an instance of it I know that by doing a static call which I show you here that I that I'm basically gonna get it so the way that is is gonna work is as soon as I make this call it's gonna check and see okay does that instance exist if it doesn't exist it creates an instance and it also creates a game object with the name of the instance type that you're trying to create so you know that when you call this method you're gonna have an instance in memory so that's what this is doing okay so now that that is done the other thing that I wanted to do is say for the most part the web services that I've been dealing with over the past few years have been communicating with Jason so when I say Jason if you don't know what Jason is here's an example of a JSON file is basically a structure is this basically structured data that it's not as Barbossa xml so just make sure that you read on Jason I think I should do another video that goes into more in depth of what JSON is but if you don't know what JSON is look it up and then come back to this video so that's basically how I'm transmitting there am I'm requesting JSON data and I'm also posting JSON data so just know that that's what this default content-type is gonna be if you want to change it you can change it as well you can change my implementation if you want to allow for more dynamic content types even or even be able to pass it through one of the arguments of these methods so the next piece of these is I wanted to show you some of the some of the metals that are have available so I show you that I had the HTTP GET so I'm just gonna collapse them all so you can see them all here so the first one is gonna be the HTTP GET hey like I said it takes URL it also it also takes that I call back so this is gonna be the matter that you're gonna be that this is gonna be calling when it's completed so the callback can be anything that you want you might be deleting or actually getting a player and when you get that player you might want to update a screen so in those cases you want to you know have a callback to a meta later on you might need to update your canvas or you might need to do you know something else that you're doing in your game logic that might need to be executed after the gate gets called the other thing that I wanted to do as well is I wanted to include a delete because you're my you know you might be deleting a player you might be deleting you know some temporary data that you are hosting or you're storing in your web service so that's why I require I include at least one it also takes a URL and it also takes a callback the other thing that I wanted to include it was an HTTP POST like I explained in the beginning of this video this is normally for inserting data so if you wanted to create a new player so you were passing you know the URL for that player endpoint this is the body so you need to specify you know you might need a username you might need a password or you might need an email you might need you know some additional information about the player I also have a callback here just like I do on these other ones but one of the additional parameters that are having here is headers reason why I included headers in my case is because I needed to include the client ID and the client secret which is very common for for web services and then the last one was gonna be a port I might need to update the player email or I might need to update the player statistics so whatever it is that you're trying to update it's normally done in a put and then it also takes a URL and it also takes a callback so now let me show you some of the implementation so the implementations and most of these ones are very similar there are actually very simple so normally when you're when I'm dealing with you know HTTP requests and you see in the unity web request which comes from if we hover over here you can see that it comes from the unity that networking and this is generous implementation for making web request over the web so normally the way that this works is you have to do the using because this is gonna have to be leased pose at some point so the scene that says you just using you declare your variable your type which in our case we're gonna do a web request we need to say that we're going to so there's a static there are static metals that are available in the web request and just like I show you the ones that we implemented are again the other one's gonna be delete yeah I was gonna be post and then also output but unity also provides you with a head I haven't really needed to use a head just yet in all the stuff that I've been doing but if you need to do a head you can also include that one and then they also have some other metals that are available here such as you know doing if you wanted to escape a URL if you want it to serialize a form and then you know there's other verbs in here that are available as enums so for now I think I only need to get and then the ones that I just mentioned so what I'm doing is basically wrapping unity functionality into a metal that we can control it actually makes it a lot easier so I don't have to know all the details that unity needs in order for me to do an HTTP GET so so like I say the syntax is gonna be the unity web request and then the variable name equal the static meta which in our case is gonna be good and in the URL we're passing in so the next thing to keep in mind is this is gonna be an enumerator and the way that I that I did this is I did it as an enumerator and then I'm basically doing a yield return web request and then the way that this is gonna work because this is gonna be in a synchronous operation and you can see that by you looking at the comments here this is actually returning a unityweb request a sync operation so the way that this is gonna work is this is basically gonna heal until so it's gonna return if it hasn't if it if it hasn't completed the only time that it's gonna that is going to be done is as soon as we get to the as soon as we get here and if we have for whatever reason we have an HTTP work here I wanted to return an error with a callback so remember that action that I told you that we were specifying here and I was doing on request and request complete and passing the request if we go back into here I'm actually calling that callback so this is gonna be the method you're passing as an action so it's gonna be a pointer to your basically to your meta so in this case I have an error so I the callback to it it's gonna be a response it's gonna have a status code and also the error that I'm getting from the web request and this is gonna be an object that I recreated and this is one of the models that are having here I'll show you some of those in just a minute the the other instance in here if let's say that everything was successful successful we livin in error but the request was completed then in that case I'm checking to say okay what's the web request completed if we did complete it and we didn't get any work here I try to parse the error there data back so the way that I do this is I get the downloaded Handler and then there's no property there called data I convert that to utf-8 and then I get the straight representation of that and then I put that into a variable called data then I call I do my callback which I use the same object I get the status code I get the error just in case there was an error which in this case they're my no be an error because I was checking for that here but I put it in there just in case and then I'm also filling it with the data data that I'm converting so that's how the game works now if we go back to the HTTP delete so in this case I'm calling the element of the unity work request provides and a pass in the URL I do the exact same thing here I do a same work request and then I check to see do I have any errors if I do I you know I make my call back and then if everything works and we're completed I change I basically same back to the callback a response object with the status code that the webserver is returned so the delete is pretty simple and then if we go into or HTTP POST so this is the case when we might need to insert data normally that's where you'll do in the HTTP POST so the first thing that I do I convert I compare this information to Jason and I think I'm doing this twice to be honest just by looking at my implementation because what I did here isn't converting these to JSON already so I don't need to do it I don't need to do it here one more time so I'm gonna in the end of changing I'll make a change and then check it in so I'll just change I'll just change it to body and I don't need to do this again because it's already been changed and then it'll just rename these to be body and then body right here hey I think yep so the way that is it gonna work is I'm also passing in URLs I can show you the party is gonna be already be in JSON format so what I'm gonna be requiring is that you convert the data to JSON ahead of time and then if you haven't done that then this is probably not gonna work and just know that this is gonna have to be Jason just for now and then I can have more options later and then check to see what data you're trying to sign and then serialize it to the data that your web service is gonna need so for now I'm doing the exact same thing except that I'm calling a pulse I'm passing in the body so if you look at the post implementation it takes in a string which is gonna be the post data in our case is gonna be in a JSON data that is serialized and then the other thing that I wanted to do is I wanted to add a basically request header so this is also my own type I'll show you that what that is but all that is is actually just a key in a value object and what I'm doing is I'm saying okay if the person that it's making a call to that should be post didn't request headers then I skip over this if they have headers then I go in and set the header on the web request so I'm basically gonna go through and loop through each one of the headers so let's say in the case where we're expecting the client ID so in this case I'm specifying a client ID in a secret so when what I ended up doing is I needed to so if we go back to unity I needed to pass in the client ID which is actually this one and then this so what I ended up doing if we go back into my rest web service client and what I'm doing is I'm specifying specifying that information here as a key in actually as a value and in looking at it right now I think I'm gonna have issues with the implementation of that because let me look let me look at something here and this is actually great because I'm troubleshooting so if we go here and we look at all my examples so I did an example on the MDS your vision API in the way that this works right now oh it's actually gonna work right now because this is actually the implementation ID previously on another example that I did where it was a console application so the header the header was actually the key was named OCP and then I sure has a weird naming convention but this is actually the the name of the key in the end of valor was gonna be this value so in our case that's what I'm passing in and passing in a key in a value which happens to be the key that I'm passing in through the example in the in the variable it's gonna be that and in the father it's gonna be that so I think we're good I was thinking we needed to specify a different key in value so this actually is gonna work so the other thing that I needed I needed to specify was gonna be the content type that I'm gonna be posting to so in this case is gonna be application Jason and then so that's what I'm doesn't that's what I'm actually specifying in here and then I also needed to include the date data that I was gonna be uploading and you really has this where uploader upload Handler and I say we're is because to me this is not raining - what if but this is a way that it works is the way that they have it so if you need to include body data on a post request this is the syntax that they have they do web request upload handler and I'm imagining that the handler is gonna be data that we're uploading and it's just different to what I've done in the past but know that that object is the object is gonna hold the data that you're posting on the body so I'm doing the same thing and converting the two bytes because if you look at these upload handle raw it takes a bi array so I'm basically getting the bytes out of the body the body was a string so if you want to convert the string to bytes there is this there's this utility in system that takes encoding the you to you you TFP and then give bytes and you're passing the string value so that's gonna get you the bytes representation that we then insert into that object and I apologize if I'm talking a lot it's just there's a lot of different concepts in here that I feel like I need to explain so once we have the data in the body of the request which is what we did here then we do the same thing that we did right above it we need to actually call the same the same work request which is gonna be in a sync operation we do a yell return with that method in it then we check to see okay that we get any errors if we didn't get any errors then we should be good to go if we did get errors we're gonna call the we're gonna do the callback if everything work okay and we have basically completed request then I try to get the data out of the out of the don't download handler just like I did right above it because a lot of times when you're doing a post you might get data back you might get an ID that was updated or some information about the post request and that will vary depending on the specification of your web service so you're more than welcome to change this implementation but if the implementation is returning data you're gonna get the data back here and then I'm also doing a callback just like I did right above it and then the last one is going to be a put a I'm also doing an object body let me make sure that I keep it the same way yeah I think I think in this case I'm gonna do exactly what I did right above it I'm going to require you passing a string and then this is just gonna be body and there was just nobody here just to keep everything consistent and then the only difference between the previous method and this method this is gonna be normally for updating data so I'm allowing you to basically do exactly what we did above it you may also need headers I just didn't feel like it was required but thinking about him now I'm going to I'm going to be had in here because you might need to specify headers as well on the put and then just basically just gonna copy the eave headers is not equal no then we're gonna be setting the headers right you know what right when we're creating the web request and then we could probably move these to meet Samantha later on for now we can just duplicate it and then paste it so I'm gonna allow headers to be passing on the put and then we might need it on every single one of them to be honest because for the most part I think I delete I don't see you needing headers on the again you might need the specific headers but let's just keep it this way and then we can get more features if we need to do so on the HTTP put everything is the same as opposed except that work that we're calling a put we're now also checking to see if we don't have headers define if we do have headers define I'm gonna set the headers on the web request so and then I'm doing everything else you know it's everything else is gonna be the same so just to prove that this is working and just gonna make a call to the the as your vision API I actually hit my quota so I might not have enough quota to be able to call this but let's see if this works if it doesn't work we should be able to at least call and you can see here that I was getting an accessing I think it's because my trial expired but let's just call it again and see what happens but before I call it I want to have breakpoints and I want to show you how this works because I think there's nothing better than showing you the debugging execution of code and that works for me I think it makes a lot of sense when I debug your code so I think it's gonna help you in understanding how this is working so I'm using via a visual studio code and also the unity debugger but you're more than welcome to use any other editors okay so now that I have debugging turnout and gonna hit play and this is gonna allow us to debug it so you can see in here okay so the first thing that I do and I can look at my variables here is I know that at this point I you know I have a half of this which in this case is going to be the instance of restclient example so if we go and expand this this is gonna be a game object so we can we should be able to see everything that we have here available so I haven't defined I haven't declared the these variable just yet so if you look at client header which are you saw and I lost and let's just go okay it's right here so if I step over you can see that I now have a client header and we can see that we have the key in the value so we know that that is working the next piece is gonna be an image URL so if I step over I should also have an image URL which is the object that as your vision API takes and this happened to be the image that we're gonna be reading the text from so the next thing that I'm gonna do is I'm gonna be stepping into this and I'm gonna show you the single tone execution so right now if I hover over this instance it's actually hasn't been credit just yet so if I go ahead and so instance is gonna be no the first time it looks like it wasn't no because it was created previously it looks like it yeah it was created already so that's what I was trying to decide why I was getting so if we look at the instance right now we we do have an instance because this actually came back with the variable and it was actually created so regarding step over step over and they looks like we let's see let me go into it and I'm gonna go back into unity okay he play hey let's see make sure okay so looks like a success in I because the subscription key or wrong API endpoint was what's called that's fine we got we are unauthorized but I think it was working the piece that I wanted to show you is the as getting into a poll so what I'm gonna do is I'm gonna put a breakpoint here so we can get into it so let me go ahead and hit play okay so we don't need to go into that anymore and then I got a break one here he play and then so we we were able to call the HTTP POST I can see the URL that we're calling we can see the body of the data that we're calling which is interesting because I did passing I did pass in some data so let me go ahead and stop it and see what the problem is and if I look in here this is gonna be the image URL which I I did pass into JSON so what I'm gonna do is gonna add a breakpoint here I'm gonna hit play I'm gonna make sure that these works so let me okay so I'm gonna go ahead and play it stop and play one more time okay and I'm gonna do these I'm gonna do these as well and then what I'm gonna do is I'm gonna hover over this today and I'm just gonna copy these so this is one of the cool things and features about code is I can I can say okay valuator a it says known indentify or json even though i'm telling it to convert it to Jason let me make sure you know an identifier the other thing that I could also use is we can use the you can do it this way to want to make sure that Jason utility is available for some reason the Jason utility which it is available at this point and I see it they see immature L so I'm gonna step into I'm gonna skip play and the body it's not set gesture which is really strange and that might be one of the issues here okay so let me let me change this a little bit okay so what I'm gonna do is I wanna make sure this is being serialized so I'm gonna say it's gonna be image how can you say JSON data and then let's just do that and then to JSON this takes in an objet okay so this should work let me just call Jason here and he takes in another lo let me make sure that let me look at the overload here oh this is whether I want to pretty print or not okay so we don't need that so let me go ahead and add a breakpoint here let you know we can also do is that the Balkan log and there's some data and I'm gonna be honest with you I haven't tested this because I just wrote it all and then you know just took me today but I don't have so many times that I feel comfortable with it but obviously it's not working so it's okay well we'll get it working the other thing that I can think that I can think of as an issue is this object is not serializable and that's why it's not serializing so that's probably what the issue is let me go ahead and start the okay let me stop here and it's stopped these from the debugger do it one more time and there we go so if we got in okay so I think that's the issue because it's not converting this so what I'm gonna do is I'm gonna flag these and this is gonna be system that's serializable and let's see if that makes any difference normally that's what the issue is and we can hit just gonna hit play to stop it and then hit play to play again step over okay we still don't see image URL as serializable let me make sure this is public class image URL to Jason and Jason utility and this is really strange because normally these type of thing works okay so let me do one more thing and I notice that I had issues with properties in the past so let me go ahead and do this change it to be just a feel damn I'm gonna hit play and I don't this a million times and then of course it doesn't work when you wanted to show somebody so that's normally the rule for developers and if we hover over these we can now see that data so yeah that was my that was my concern is I think the issue is is basically that these needs to be a field let me go ahead and make these no serializable just to just to know exactly what the issue was okay now what I'm gonna do is I'm gonna hit play to stop it and then he played to started again so we can look and see if these yeah and that was Asia so you can see that I can is now being serialized and now if we go into here and then we we should be able to see that in the address and as this is done then I'm gonna hit play to go into my request and then I should see the data now and then what I'm gonna do is I'm gonna step over and in this case we are passing headers because then we can see both headers been passed in now if I go a step over he's gonna set the headers on that what request okay ensure we can probably find those headers in here if I go in a look at some of these variables see a floor handler I don't know where unity puts in content type and then so it's probably here somewhere anesthetic members you're right okay so I know that it's going through I just don't know which property but that's how you set headers and then if we step over we can so now we're basically setting the body and we have the body set correctly now I can go through I can step over it's gonna be a sink so we won't see the response until later on and what I could do probably is just put a breakpoint in my callback which happens to be this one right here and then if we go back into unity we are getting so you can see that the data is now getting this play this play correctly so let me do this one more time and then we can go in and make sure or callback is getting cold okay so going into that I play hey I want to see if we're getting you know totally spoiling right here and then also two is born right here you play and I think what's happening is this is not giving us a network error is actually throwing up or not rise right away and because I'm not capturing that it's basically arena so what I'm gonna do is instead of doing calling that IP I want to show you how easy it is to make your own API so if you haven't downloaded did that make core tools so I'm using that make or you can probably some on any you know you can do the same know that you can do these in you know in another type of web services technologies so I'm gonna use that Nate core for this so what I can do is I can say okay I want to create a new web service and it's gonna be it's gonna be of type Web API so it's gonna say okay that a new Web API and it's just gonna create a it's just gonna create a web service for me and it happened to choose the option which is called videos let me see that yep any we have we have a API called videos so the other thing that I can do is I can say okay let me run and it's gonna run a web service for me in I'm pretty sure there's a control in there the aromatically has an end point for us so I'm gonna say let me go into that code you can do I think I police all the videos and then what I'll do is so open a new version of code and then we can look and see what the controller is yeah so so by default that make or adds a valid controller and this is a controller that we can use to test all of these this requires that's a bill okay yeah sure we can speaking whatever whatever he needs so what I'm gonna do is I'm gonna have a breakpoint here I'm also gonna add a breakpoint here and we can also do one on the pose one on the PUD and then also one in here perfect and then what let me make sure so this is just to show you that you know we have a web service listening that port so if I go here and I do 5,000 and of course this is let me make sure this is listening on HTTP or you know you think it should be it's fine and then there was a values there was basically a values in point and yeah I know these we can just say con save and let me see what the what the default route is going to be which happened to be okay so we need to include the API and this is our endpoint so this is a one receiving so you know this is working because we're returning the so the web server is returning this is working fine so what if I wanted to debug this and see and make sure the unity can call into it so this is what I want to do I want to I want to go ahead and attach it because I'm already running so this is gonna tell me okay which which one do you want to attach it to let's go ahead and do this instead of attaching it so what I'm gonna do is I'm gonna stop I'm going to go ahead and go into my console I'm gonna kill this go back here so what I'm gonna do is I'm just gonna say you know what I want to I want to attach core or what I could do instead now that I'm looking at it I'm going to run it there we go and it'll attach itself I think that's the easiest option because I agree now which which ID was the current process okay so now we're at actually longer so I'll show you if we go back into our unity and I go into here so let's test the kit so I'm gonna do so I show you the example here and I think they should work fine so what I'm gonna do is instead of modifying this example I'm going to create a new example it's going to unity and in this one we can say okay this one is going to be risk line demo this is going to be for the first example and what I'll do here I'll just say risk line demo we're gonna say risk life as sure demo and this is this is gonna be the one that we're using for a year but let's say that we wanted to use two again there was you know simpler we can just do this other demo and this one's just gonna be risk lying okay let's call it simple again and then reload it's gonna change the scene and then what I'm gonna do in here I'm gonna change this example so I'm gonna go into my scripts and it's gonna duplicate this and then we can just say res client is the risk line example set example oh we can just say clicking acting get is fine and then I can give you more options later on okay so this is gonna be for that let's go ahead and go back into the code and I'm gonna go here and okay this one's gonna be good example and the URL is gonna be different this is gonna be the one that I'm running a local house which is gonna be this one so this is gonna be the one that you're running your web service from we can just do that a yeah I think that's fine in this case I don't need a client ID and I don't need a secret because we're keeping it very basic so I need these I need to set up any headers I don't need to set up an image I don't need to set up a JSON data so it's basically just gonna be I get a very simple game a song and then in this case I'm just gonna do HTTP it should be bigger we're gonna pass in the base URL and we're not gonna need the only thing that we're gonna need here is gonna be the callback and then we don't need headers so it's gonna be fairly simple hey let me see what I'm missing here just to make sure okay you need one more parenthesis and then we have our own request complete we don't need this game at URL I'm just gonna clean it up so when I check it in you have the you have the latest code just recommending okay so this is good it's gonna copy these and now go back into unity and then we're just gonna change the subject a little bit so this object is going to it's still gonna need a rescue op flame because that's gonna be or Singleton oh and that's why my single turn I say because I had the monobehaviour associating okay I'm thinking about something else not okay so I'm gonna have a rest client get example which is all one that we just created and this is the only thing that we need the other thing that we're gonna need is we're gonna need the route and normally separate those two I have not only the base URI but I have a route that I'm gonna be calling too so I'm gonna go into here and then I'll just - we can just do a string interpolation here and I don't need I don't need a forward slash because I already have it and then we can just - we can just do it this way alright so we're just gonna call these API and then it's gonna be the route now we're calling and then I'm gonna add a breakpoint here let me also add a breakpoint right here because I want to show you how everything works okay so I think I'm good to go there and then I go here gonna touch the bugger so now I should have two debuggers running one it's gonna be in the web service side and then the other one is going to be in unity so now let's go into unity and before I forget I'm gonna do one more thing go to file build settings and you're gonna have pieces and open scene let me do the same thing on the other scene this is so that you have him if you want to build them to your platform and then I'm just gonna move this one up okay so we should have the two there all right so I think we're good to go let me go back into my other example okay so we're good to go so then he play and everything is gonna start executing so this is the new example that we created using the get and we're using our singleton and then I play we should be heating or get meta which we are the URL should include the route that we're calling which it does and then I'm gonna step basically when I hit play I should be hitting or web service so let me make sure that we are hitting the web service looks like we're know let me see if we got any errors in the console and unknown so we did hit an unknown error so I found the issue of why we couldn't call the HTTP web service so what I need to do if I'm using that make or I need to disable HTTP redirection if you want to use HTTPS you would have to look at unity documentation for including a certificate so for now I just removed that in the end the other thing that I needed to do as well was removed the HTTP from the pattern and then the last thing was let me make sure that I think that was everything to be honest so now if I go in and execute the web service we should be able to look the data and call the endpoints without having to do HTTP so you can see that we're starting with five thousand which is the HTTP port and I can do a work call and if we're looking here in a GUI TP now start is clear we still be able to call it so Nut Bars working so now if we go back into unity and when he play so the poor that we're calling it's gonna be the wrong port so let me go ahead and stop it there's gonna be five thousand in they need to also do five thousand here which is gonna be for HTTP and then I need to change this to be just HTTP you play to stop it okay come then then let's go hey okay so let's say now let's go ahead and do the same thing here so just HTTP and that's the port okay so now I'm gonna hit unity and then we should be able to now call the other web service okay unless he play and should be able to replay he play in our hitting a web service play with your back into unity there's no network error we are completed and now we're getting the data back which is value 1 and value 2 and then we should be able to now call or call back but if you're looking here we're getting you know value 1 and value 2 so now I put a breakpoint on here in this one which actually didn't trigger for some reason I think it's because we're doing everything as synchronously but I can see that the data came back and also they status code so let's go ahead and do this one more time so you can see how it works so if I play I'm gonna call my get meta so this is gonna go ahead and call the get method in the unity networking and then we're calling a web service we're getting data back and then I'm making a call to the callback which basically lands in here so that's all working just fine so if we wanted to say that I wanted to also call let's go back in here let's say that I wanted I wanted to do I get with you know with an actual ID I could basically specify an ID the other thing that we could test as well and it'll be the last thing that I check in this video because we could actually do a call on the on the post so let's go back into our example and for unity and gonna go here and this was gonna be forget and I think for this one it's fine we can just do another call here and I'm just gonna do a post we can do it should it be and then post and then if we look at the Rao let me go back to the route it's gonna be the exact same API valleys but it's gonna take a body as the value so this is still gonna be valid and then we're gonna need to do body so on the body we could probably just to just do a string here and from this one I'm going to it's go ahead and do an object so that I yeah so make sure that everything it's gonna work so I'm just gonna do we can do public class this is gonna be C we're posting a player for instance and let's make sure that we give some properties to the player we just say full name I think the dimple name is fine and then what I'll do here I'll just say Jason utility dr. Jason and then we just create a new player and I will just say for me it's going to be John hey I think we need we have a parenthesis there and then here's a callback and just covering at least 20 lines so you can see everything so so everything that it takes to call so what I need to do I can't call anymore so I'm just gonna just gotta comment out that one and then we can just add another comment here this one's gonna be st. a post request hey we can go ahead and you can just this one here play again and then what I'll do is so you see play here go back into okay so we have a breakpoint there so the way this is gonna work if we're gonna hit play it's gonna call a pulse we can see should be able to see full name yep which were seen also a URL and then or call back and then if I hit play I should be able to see so this work successfully and then I'm getting basically by it's back in the data so this is con supporting media type because I'm not telling it what kind of data I'm gonna I'm gonna need so right now this is saying from body and then a string so let me make sure that these posts is going to work which I'm deciding how I say which is going to be application type and then so then the issue here is that we are not telling it what type the response is taking so right now it's just a string in a use of value I will need to create an option in here in order for it to parse correctly so if we want to just to test the the idea of this working we can go back in here and I say what's happening so let's go back into the this example will show you how to make it work with the complex type but for now just to test these we can just say you know what the web service requires that we just passing you know just a value which is gonna be a string we can just say John Doe which happens to be a string value so you say ok we're gonna say in John down we want to get John Doe back so he's gonna say play and then go back into unity I'm gonna hit play to stop it hey let's go ahead and hit play one more time and then I'm just gonna hit play you play again hey this is now trying to get the data back hey let me make sure that is hitting or web service ok so I think I know why the post is not working and it's not working because we haven't specified in the header and I'm glad that I did made it optional but in this case we're gonna need it for the post so if we're going here and we look at the object so it's gonna be for the core routine but I'm gonna do exactly what we did on the other example I'm gonna need to pass in the request headers so I'm gonna go in and look at the example that we just created and then I'm just gonna copy the request header snip it go back to or new example this one I'm gonna pass in couple of things that we're gonna need so if you're gonna need we're gonna need to specify the content type there's gonna be content type and then this is gonna be application Jason so also we're gonna be putting in the headers and then I'll just go ahead and pass in a new list and then this one is gonna be request header just like we did before and then inside we'll just put in then you header except this is gonna be just headers I just say call header and then pass that in here and make sure that lays cannot be found because I haven't heard of the using statement alright so let's try and see if this is gonna fix the issue I'm gonna go back into the Bachmann run this gonna go back in to make sure that I'm still debugging so yeah I'm here okay let's go back into unity you're gonna hit play to stop it and play to replay the test okay so now we have a request header of content type I'm going to go ahead and do actually step in step in and then we're now hearing the post which is John Doe so I think that the thing that we need it was the headers is because the web server is nothing now what content types you're sending to it so needing the headers of being content type in obligation Jason was one of the requirements on the web service so hopefully this gives you know enough information to be able to call you know a post and also again one thing that I need to do here because it's gonna be risk lying get and post example so let me rename these really quick before I call it good it's just gonna rename these guys as well okay and I think this should work and then last thing I'll go into unity and fix T fix the scene name and also these moral behavior so this one it's going to complain because they're now renaming and that's fine we can just go ahead and create it well this one should be fine yep it's gonna here remove well yeah worried you to be okay so that looks good and then I'll just rename this then we're going to the project and this one is gonna be get in post so let me okay I think I see where I was complaining because I was renaming while I was changing okay they seem me for some reason didn't rename me just rename this too so the other ones go because yeah we rename this one and they let me go back in here I'll fix this one more time and we should have HTTP this should be okay and then we should be okay with the script as well so let me go ahead and play this just to make sure that everything is working and then now okay that's because I the web service is not running anymore so I'm just gonna run it and we should get two responses okay so we're now running go back into unity you play and it should be your first call which is again so that's working and then or next call which is a post which is also working so that's everything that I wanted to show you guys if you guys have any questions please let me know and also be sure to check out github or I'm gonna be posting this code thank you guys alright nice to know much for watching this video really appreciate your time and if you have any questions about what I just show you and mention in this video let me know in the comments also be sure to check out game Dave down there because they have amazing resources program developers and also find the in pictures that come when posting information about what I'm doing behind the scenes and also early access source code thank you very much yes
Info
Channel: Dilmer Valecillos
Views: 19,177
Rating: undefined out of 5
Keywords: Unity, Unity3d, C#, Programming, APIs, JSON, Serialization, Deserialization, Using REST, JSON in Unity, Web APIs, Web Methods, Endpoints, Game Engine, Game Development, rest api, json (programming language), json api, unity 5, unity www, unity request, unity curl, unity ajax, unity web, unity web request, web request, unity api, unity and api, api request, api request api, rest api unity, networking api, dotnet core, json, jsonutility, get, post, put, head, network
Id: dGrgy2dAvSI
Channel Id: undefined
Length: 57min 27sec (3447 seconds)
Published: Tue Aug 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.