Monster shadow

VIDEO

Sending Web Requests in Unity - UnityWebRequest

Description:
Sending a HTTP request in Unity can be a little tricky. Watch this to avoid a few gotchas and streamline your development.
ADD A COMMENT

Nam Ho Koh
Thank you so much for the tutorial. I was wondering whether it would be possible to bypass the SSL certificate verification when making a POST request.
Sacqlain Brown
Or even help me understand it
Tarodev
What do you need help with exactly? What are you trying to achieve?
Sacqlain Brown
Hey I have a project I’m working on that uses UnityWebRequest and I’m stuck could you assist me?
Omer Bentov
Tnx for the video. I used unityWebRequest class as you did and it works perfectly on the editor. However, after export to android, my server doesn't get any requests. Do you know anything about this bug?
Omer Bentov
@Tarodev LOL it isn't localhost but tnx ill try to figure it out with your advice.
Tarodev
Hmmm... The first silly question would be: are you using localhost during testing and the Android device isn't connected to the network? Then I'd move on to seeing if the Android device can make a request to an external api like https://jsonplaceholder.typicode.com/ If it can then we may be looking at a cors issue on your server or something. If all this fails then it could just be unity messing up when building, try swapping between mono/il2cpp to see if it makes a difference.
srisrid multi purpose channel
Good vidio
SEE ALL COMMENTS


Transcript:

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