Sending Web Requests in Unity - UnityWebRequest

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so recently i was working on a project that had a server and i found it absolutely impossible to find any decent information on the base unity web request library i didn't want to use a third-party library but i have to say i spent quite a few hours getting this to work the information is just not out there on the internet so hopefully i'll save you a few gotchas and i'll teach you how i implemented it so for what is simple in just a traditional c-sharp project like in asp core or something unity honestly adds a hell of a lot of complexity to their implementation so what i've got here is a get request and i also thought i'd just show you a post request as well so the made in potatoes is right here i just created a method takes the url it takes the request type which i've just made a an enum and then also an optional data so if it's post you'll obviously include the data so what you do is you create the web request and you put in the url and the request type if there's data i am converting it to json with just the the base json utility that unity provides and then turning it into a butt array and then basically the way uh unity web requests work is you attach an upload handler and you can also attach a download handler and then they're the things that you reference when you want to grab the results of the of the uh of the request so what i'm doing is i'm just attaching a new upload handler and i'm sending in the uh the byte array that i want to post if it is a post request and then tr in the same way i'm attaching a download handler and i'm just setting the the return content type uh if you need to also attach headers like an authorization header or something you can simply once you've got your request you can say um i've created this little function down here attach header so i'll just say like attach header i'd send in the request and then i'd say like authorization and then you know the token for the for the authorization or whatever it is i don't need that for this now there are a few gotchas here so uh when you're deserializing the the result from the download handler this took me a long time to work out so down on my to-do object traditionally i make my properties just like this and i'll have my you know my my uh my getter and setter there uh so you you cannot have a getter and setter when when if you want to deserialize using the the json utility that unity provides so remove that in addition you need to match the type case exactly to the server so if you're using like json.net in c sharp uh you can have capital case here what's it called uh capital case and then even if it comes through as camelcase it will still deserialize and it will be smart enough to map the objects but with the unity deserializer you need to match the the type case exactly all right so here we go we're creating the request we're sending it off and we've got this in an innumerator so that we can wait for it and then we're deserializing the result and then in the same way we're just creating an object to post we're setting it as post we're sending it waiting for it and then we're deserializing it and if you follow those tips exactly you should have your web requests set up and good to go hope this helps
Info
Channel: Tarodev
Views: 20,801
Rating: undefined out of 5
Keywords: Tarodev, Unity, Unity3d, UnityWebRequest, Http, http request, web request, request, c#, unity tutorial, tutorial
Id: K9uVHI645Pk
Channel Id: undefined
Length: 4min 11sec (251 seconds)
Published: Mon Feb 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.