How to Make HTTP Requests in Unreal Engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone in this video we are going to go over how to make http requests in an unreal engine project without the use of an external plug-in so to get started i'm going to create a new c plus plus project c plus plus code is required if you don't want to rely on a third-party library but you can still make http requests and blueprints natively as we will discuss later in the video once the project is created and opened in both unreal engine and visual studio let's edit the build.cs file to include not only the http module but also the json module this will allow us to perform http requests against any api using code from unreal's http classes as well as parse json data for those http requests and from their return responses using code from unreal's json classes and we can do all of this from anywhere in our project and code that can be executed at any time in the game's lifespan so for this video i'm just going to select the gamemode class and override the start play method because i want to send the request to an api right after the game starts but in order to access the http module code we need to include the http header file and another function we need to create for this tutorial is a handler for the responses we get back from our request it has to be of return type void and take in three parameters the first one is of type fhtp request pointer second one is of type fhtp response pointer and the third and final parameter is of type bool and let's generate a method stub for this one as well now let's actually implement these functions starting with start play since this is a inherited function let's call the super class implementation first next let's create a fhtp request reference using the fhtp modules create request method and this request object can be configured in any way a regular http request can be configured for example you can set the function that gets called when the request is complete the request url any request headers the request verb and more first let's set the request on process request complete delegate what happens after the request is done whether it was successful or not and we can do that by calling bind you object on the delegate and passing in that function that we just created earlier next let's set the request url what is the address of the resource we want to manipulate so for this video i'm just going to use a dummy api that's publicly available to everyone it's called json placeholder link will be in the description below and for now let's just use one of these get endpoints lastly let's set the request verb is this a get post put patch or delete request this one in particular will be a get request and once your request is ready to be sent call the process request function to actually execute the http request moving on to the on response receive function after the request finishes and a response is returned by whatever server our unreal client tried to request a resource from whatever function you bound to the on process request complete delegate will be invoked with information about the request we made the response we got back and whether or not the request was successful and this http response object has all the information that a standard http response has for example you can get the response headers the response body etc for this video i'm just going to print out the response body once we get a response using the ue log macro just to show that we were able to run an http request and retrieve some fake data from this test api successfully once that's done let's save and compile our project i personally use live coding so i'm going to go to the editor and press ctrl alt f11 after compiling if we run our game the http request should have been made right away and we can see if it processed successfully by checking the output log and comparing the response we printed out to whatever we expected to get back and at first glance everything seems to have worked fine but let's say you wanted to process the data further in your unreal code not just displayed in the logs for example what if i wanted to set some variable in my game mode to the value of this title field in order to do that i would have to parse the data returned which is usually in json format so to do this going back to visual studio we need to use code from unreal's json module which we already added to our project's list of dependencies but now let's include the json header file and the idea is to convert that json formatted string that we saw earlier into an f json object that we can extract specific fields and values from to do that we have to create a t json reader object using the t json reader factory create method while passing in the response body in string format once we have that we can call the fjson serializer the serialize method and that json object we pass in will be populated with the data from the response if the response body was formatted correctly after that we can now retrieve the value of any specific field from the response body by using the appropriate getter method based on the supposed fields data type and passing in that field's name to keep the video simple we're just going to print out the title and just an fyi that this fjson object data type can hold all kinds of data not just strings but also numbers arrays bools objects etc and if you're not familiar with the structure of the response or the response body schema is constantly changing instead of the getter methods you can use any of the try get field methods or the has field method in case you're ever unsure of whether or not a field exists moving on let's test out our new changes by saving and compiling again and if we run the project again we'll see that the deserialization of the response body string into a json object has worked but now let's say you wanted to attach data to your http request body for the api to use in some way before responding back the way to go about that is similar but the opposite of what we just did going back to visual studio basically instead of creating a json object out of a string we have to create a json formatted string out of a json object so first let's create a new json object and customize it to include whatever data that we want to send to whatever api endpoint through the request body for every getter method that the fjson object class has there's a corresponding setter method so we can theoretically send all sorts of data in the request but in this tutorial we'll just keep things simple and create a json object with one string field called title with the value of foo and to convert this to a json formatted string we need to create a t json writer object using the json writer factory create method while passing in a string variable then pass both the json object and the songwriter into a call to the fjson serializer serialize function and the string you use to create the json writer will from this point on contain the json object in string format to test this the json placeholder api we've been using has a post route that just simply returns back whatever we sent in the request body so that route is just a website slash posts nothing after and of course we have to change the request verb from get to post in this case in addition since we're sending data in json format we have to specify that in a request through the request headers by sending the content type header to application slash json and finally we have to actually set the request body using the set content method let's save our changes compile again and if we run again this time the post endpoint will receive our request and that should just return whatever we sent along with an automatically generated id which it looks like it did an object containing a field called title that has a value of foo the last thing i want to say in this video is with regards to blueprints the functions we touched upon today are not exposed to blueprints natively but you can still execute any of the code that we use from the http and json modules and blueprints like you can with any c plus plus code it just requires a little extra work for instance you can make a c plus plus function that calls http and json functions then tag that c plus function as a u function with the blueprint specifier there are many ways to go about it but that concludes this brief introduction to working with http and json and unreal engine a link to the code written in this video is in the description below if you found the tutorial useful then please like comment subscribe become a member of the channel support us on patreon join our discord server follow us on other social media and special thanks to our higher tier patrons and logical cuber sherwood 2142 michael comiso graeme devine dwight everhart mark wetch morgan heinemann bioblaze payne lucas moskin and rick morgan to everyone though thank you for watching
Info
Channel: Flopperam
Views: 26,292
Rating: undefined out of 5
Keywords: unreal engine 5, ue4, ue5, http requests, get post put delete patch http requests, rest api, json placeholder dummy test api fake data, request response http, c++, blueprints, json, unreal engine c++ tutorial, http response return handler, content-type application/json, request response body json parsing, live coding, varest plugin, no external third party library, fetch, calling API in unreal engine via http
Id: vLGZp5hl6qU
Channel Id: undefined
Length: 11min 39sec (699 seconds)
Published: Sun Feb 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.