Skip to content

Performing Http calls

In this post we’re going to see how to use the http module that is built inside the engine to perform http calls to various APIs. We’re going to perform a basic http call without sending any data, however I have commented out the code that you can use in order to send simple data (eg strings) or even data in the form of a json file type.

Adding the required Dependencies

For this post I have created a project using the 4.20 version of the engine. Then, I added the Http module as a dependency in the public module names in my <MyProject>.build.cs file:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "HTTP" });

Performing the http request

The http module offers an straightforward way to both perform http calls as well as react to the answer of the server. Having said that, I have created two functions in my header file, one for performing a request and one function that fires when our request has been processed (meaning the server has responded):

	/*Sends an http request*/
	UFUNCTION(BlueprintCallable, Category = Http)
	void SendHttpRequest(const FString& Url, const FString& RequestContent);

	/*Called when the server has responded to our http request*/
	void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);

Here’s the implementation of both functions:

void AHttpCallsCharacter::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
	if(bWasSuccessful)
	{
		GLog->Log("Hey we received the following response!");
		GLog->Log(Response->GetContentAsString());
	}
}


void AHttpCallsCharacter::SendHttpRequest(const FString& Url, const FString& RequestContent)
{
	
	//Creating a request using UE4's Http Module
	TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();

	//Binding a function that will fire when we receive a response from our request
	Request->OnProcessRequestComplete().BindUObject(this, &AHttpCallsCharacter::OnResponseReceived);

	//This is the url on which to process the request
	Request->SetURL(Url);
	//We're sending data so set the Http method to POST
	Request->SetVerb("POST");

	//Tell the host that we're an unreal agent
	Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");

	//In case you're sending json you can also make use of headers using the following command. 
        //Just make sure to convert the Json to string and use the SetContentAsString.
	//Request->SetHeader("Content-Type", TEXT("application/json"));
	//Use the following command in case you want to send a string value to the server
	//Request->SetContentAsString("Hello kind server.");

	//Send the request
	Request->ProcessRequest();
}

For my testing, in the Url link I used my GitHub’s username (ie https://api.github.com/users/orfeasel ). Here’s the response of the server when I performed my call:

(Click on image to enlarge in a new tab)

As you can see the response is really a json file. In case you would like to consume the retrieved data check out my tutorial on Json Parsing.

(The reason that my OnResponse event fired twice is because I performed two requests)

Avatar photo

Comments (1)

  1. Hello, can you please explain me how do you do the call if there is security that requires a token, for example an oauth 2.0.

    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top