Section 2 Unreal Engine 5 C++ API HTTP Connection Tutorial Section 2 – The HTTP Request

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign to section 2 of the API C plus plus tutorial on how to connect an API with an HTTP request non-real engine if you haven't seen the first part of the series yet we will put the umg widget together you can find the link on the screen right now or you can find it in the description also in the description you can find the link to the API before we move on please take a second to like And subscribe it helps the channel to be the algorithm thank you for your support now let's get into the video okay it's time to get the cooldown to send the request and to retrieve the answer but first of all let's take a little look at the API we're going to be using so this is the time API dot IO it's a free time API and doesn't need any API keys and therefore it's very easy to use so if we go into the API docs here we can see the option we have to choose from and I want to get the current time of a specific time zone so let's click on that this gives us the option to test it out let's just push execute here for amstrom and see what we get so what is it we actually have here well in the curl you can see the request it is a get request and get is used for unsecured retrieval of data so if you want to send information or you with a package or you want to update a database you will use the post option however you just want to retrieve it and read what it gives us so next we have the request URL and let's copy this and store it somewhere like a notepad because we're going to use it later and copied into our code in the request URL you see the ending after the equal sign that is the request parameters so in order to get a different city you need to change the parameters of the request okay and we see there is a Json type and we're going to get into json's in just a second next we have the actual response and this is the Json format Json stands for JavaScript object notation it's a very lightweight way of sending data and this is the response we get and this is what we need to parse out and begin to to see what we actually need so let's go back we're going to find the different time zones that we have that we can populate in our query other time zones we have get all the available time zones and let's test that out so this is also an API request just like before but it's already different request URL so technically you could just do a request to populate the list for with all the options and then the user could pick whichever time zone they wanted to get however this is a lot more work to to get into so it's a little bit too much for a single tutorial like this so let's pick out and copy these three cities we want and the list is pretty long so we're going to use the control left function to find those settings so here we go Tokyo and here we've got London and New York all right please observe that New York has a underscore for its space there okay that's all we need from this webpage so let's close it down all right here we are back in the Unreal Engine let's go to the content browser won't roll down the C plus classes and go into the API tutorial folder let's double click on a game mode base file and load up our c plus editor I'm using visual Studios the first thing we want to do is to include a dependencies that we need um to do that we're going to open up the build file so let's scroll down the API tutorial source and API tutorial and open up the API tutorial.build.cs so CS stands for c-sharp in the public dependency model names we can add the model we need so let's put in http Json Json utilities and let's add the umg as well so what are these modules really well they are libraries that give us a larger code base to work with basically so now we can save it and we can close it okay let's return into gamemode.h file I want to organize a little bit so let's add a public section here then I want to protect this section for functions and I want a private section for variables then I need another public section for all kind of function we need to read from other places in this case we're going to use it for functions that we call from the blueprints right let's add the Constructor foreign we can add the begin play and it's going to be a virtual void begin play override right let's put in the comment let's call it the call when the game starts and Orwell's Bond okay now we can create the body on these two next we want to include the HTTP file on the top here remember to make sure that in the H file the generated file has to come lost okay now in the protected section here we will continue to create a send function for the HTTP request so we'll create a void function called send HTTP get and this need to be a view function let's put in a Commodore saying used to send the HTTP request all right let's create the body the next thing we want to do is create a function to handle the response let's start with a comma saying handle the HTTP request response and this need to be pretty specific in its parameters so it's going to be a void and we're going to call it on get time response and the first parameter is the request itself and it's going to be an fhttp request pointer and we're going to call it request next we need the response parameter so we're going to have an fhttp request pointer and let's just call it response and the third parameter is going to be a bull called B connected successfully all right let's create the body for this and now in our private section let's create a variable to store our HTTP module so it's going to be an F HTTP module it's going to be a pointer let's call it http all right let's save this and go into the CPP file in here we can start at Constructor we want to store the HTTP module in the HTTP variable so we can add the HTTP equals so we're going to start with an amp sign and then fhttp module column colon get let's say it's going to be a get request um let's go down to the begin play function at the super beginner player sorry pretended to forget that so I usually try to put it in as soon as possible and let's go into our send HTTP get function we'll start out by creating the request so this is going to be a t-shared ref and we're going to put in I HTTP request and then we're going to put in an ESP mode I'm going to be thread safe and we can name it request and this is going to be equal to the HTTP variable that we created so we're going to use the arrow notation and create request all right so now we want to bind the response function to the request okay so request and then we're going to do the error notation on process request complete dot bind you object and this will take two parameters you're going to start out with this and I'm going to use the address of the response function so we can actually copy this and paste it in foreign now we want to know where to send this request so we will add a request set URL and in this parenthesis we're going to put in the address we're going to send it to however for right now we're going to leave it to empty because um we're going to work some on the URL before we can put it in here and we're going to do that in section three now we want to set what type of request this is and we know that it will be a get request however now is the time we let the request know what it is so all right so we add a request and set verb and within the parenthesis we're going to add the word get the next thing we need to do is set the headers for the request and the first header one is set is the user agent so we're going to put in request set header parenthesis user agent then a comma and then we're going to add X Dash Unreal Engine Dash agent now I'm going to add the content type we're expecting a return so we are expecting a Json file in return so now we can let the request know that we're expecting a Json file so request set header content type and then we're going to put in application for Slash and Json right okay now we successfully create a request we just want to send that request right so we do that by adding request and process request okay so let's just add a few comments to this okay first of all we created a request then we bind the response function to request and then we are setting the URL where to send the request well we're going to get to that a little bit later but this is where we do that then we add what type of request then we add the user agent what is a juicer agent really you might ask well the user agent's request header is a characteristic string that lets service and network pairs identify the application operation system vendor and or versatile requesting user agent that is the official answer now basically we let the servant know what application sent the request okay that's basically what it is next we let the request know what response we're expecting and the reason we put this in is to make sure that if the response we're getting is not a Json it will throw an error now this is a little level of security to ensure we're not start tanking down data that are not intended to be tanked down okay and lastly this is where we are actually are sending that request let's move on to the on get time response um here we will start out by creating a Json object so here we will add the T-shirt pointer of type f Json object we're going to call it Json object then we want to do a check if the response was successful if the response get Response Code and this code is the normal HTTP code you might have seen elsewhere like 404 for page not found Etc well there is a code called 200 means everything is all right and you usually shouldn't see it but that's what it's called 200. so if the response code is equal to 200 we can continue now we want to store their response in a string so we're going to do a const here as we don't want to change this ain't going to be an F string let's call it response body and let's store it with response get content as string okay so now we get response in a variable and now we just want to read the data so we're going to create a Json reader so how do we do that well we start right here with a t-shirt ref T Json reader and we're going to call it Reader uh in this read we're going to store T Json reader Factory colon colon create and the data we want to read is the response body if we just created all right so now we want to check if there was a successful reading of the data and everything is okay so let's make an if statement and in this if statement we're going to add fjson serializer colon colon deserialize and now we're going to add the reader and the Json object we created in the start this is a lot of process but if you go through it it's actually understandable so let's add some comments here first we created the Json object that we will be working with then we check if we are successful in our request we store a response and response button here and then we create a reader for Json and here we do a check if we were successful in reading the Json and here we can just put in line This is where we put the information on what to do with the data that is how you create a request on how to build up a response function so in the next section that you can see on the screen right now we're going to make sure that we retrieve the correct data by working a little bit with the URL and we're going to handle the data coming back through the response so jump right over to that and before you do that please hit that like and the Subscribe button and that would help me out a lot so alright guys thank you so much for joining me on this um this in this video and I'll see you in the next one take care
Info
Channel: Swatpupgaming
Views: 2,321
Rating: undefined out of 5
Keywords: API, C++, CPP, Connection, DateTime, FDateTime, FTimerHandle, FTimespan, Game Development, GameDev, HTTP, Timers, UE5, UI, UMG, Unreal Engine, User Experience, User Interface, Widget
Id: Kn1uoyIkY6o
Channel Id: undefined
Length: 15min 38sec (938 seconds)
Published: Sun Nov 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.