How to to post data in Unity using web requests, a quick and easy tutorial.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i just wanted to make this little tutorial today to show you how you can use the unity web request to post data this is useful if you're using an api that requires you to post data or often in php scripts you'll want to post data there so the first thing we're going to do is create a folder for our scripts and i'm going to make a c-sharp script i'm just going to call it web post example let's double click to open this in visual studio first up you're going to need the unity networking library now to do this you use the i enumerator i'm going to make a function for this now we're not going to need the update function and here we've we've got the function post data and we're just going to accept a string and that's going to be the one piece of data that we're going to post if you have multiple pieces of data you'll obviously add more more variables up here so the first thing you want to do is you want to create a form to take your data and now you just add the fields that you want into this form we're only going to have one field and i'm just going to call the field name and in that field i'm just going to send it the string later string next we need to make a web request now remember that we're using dot post here and the site i'm going to use for my posting it's http.org forward slash post this site's really useful if you have if you don't know about it it allows you to test a whole bunch of web functionality without actually having to have the scripts now you can see that the post data has the red underline under it that's because we have no yield and that's an important part of an iron numerator so let's add that part next and that means this will run in the background until the send web request has something back so first of all here we're checking if the result doesn't equal a success in which case we're just going to debug the error so now the else is our success and in this case i'm just going to make a little string of reading the text and then debug that out to the console so we can see the result and the final thing you need to do when you finish with your unity web request you need to dispose it or you might get a memory leak and one more thing before we move on although it hasn't shown up as an error i've just realized that i need to put some brackets here for the wwe form and of course there's one more thing that we need to do before we move on which is actually called this function let's start echo routine so i'm just going to call a post date and i'm just going to send out some text let's save this and go back to unity and give it a go now let's drop our web post example onto our main camera open our console hit play as you can see here that it's returned as you can see from the form that post data was test stream so as you can see this is now working and that's why that site's a great way to be able to see what's actually going on hope you found this tutorial useful if you're looking to post data if you did please like subscribe or comment i really appreciate it it helps me grow the channel i hope you enjoy your game dev journey you
Info
Channel: Destined To Learn
Views: 5,423
Rating: undefined out of 5
Keywords:
Id: Z86Htxgi6eQ
Channel Id: undefined
Length: 5min 15sec (315 seconds)
Published: Tue Mar 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.