How to make an HTTP Request from Unreal

Is there an easy way to connect unreal to the internet?

Something like that :slight_smile: I don’t know what level of answer you’re expecting so if the code below is too complicated then just tell me and I’ll explain what it does.



void YourClass::YourFunction()
{
        TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());

        JsonObject->SetStringField(TEXT("some_string_field"), *FString::Printf(TEXT("%s"), *SomeFStringVariable));

        FString OutputString;

        TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<>::Create(&OutputString);

        FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);

        TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();

        HttpRequest->SetVerb("POST");

        HttpRequest->SetHeader("Content-Type", "application/json");

        HttpRequest->SetURL(*FString::Printf(TEXT("%s"), *UrlAddressAsString));

        HttpRequest->SetContentAsString(OutputString);

        HttpRequest->OnProcessRequestComplete().BindUObject(this, &YourClass::OnYourFunctionCompleted);

        HttpRequest->ProcessRequest();
}

void YourClass::OnYourFunctionCompleted(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
    if (bWasSuccessful && Response->GetContentType() == "application/json")
    {
        TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());

        TSharedRef<TJsonReader<TCHAR>> JsonReader =  TJsonReaderFactory<TCHAR>::Create(Response->GetContentAsString());

        FJsonSerializer::Deserialize(JsonReader, JsonObject);

        SomeOtherVariable = JsonObject->GetStringField("some_response_field");

    }
    else
    {
        // Handle error here
    }
}


7 Likes

You can also look at the Rest plugin for Unreal … depending on what you want to do.

Your post is a bit vague … do you want to load a web-page … do you want to connect to a restful web-service … do you want to make your gun run in a web-browser.

Can you provide some more information so that we can attempt to help you.

JSON Query works very well over HTTP/HTTPS with straight forward GET/POST Methods and full JSON Support.

Hi, sorry to rake up this almost a year old conversation, but I could find the solution nowhere. I tried to use the code you suggested, but I keep getting an error on the Response variable:


FHttpResponsePtr Response
Error: pointer to incomplete class type is not allowed

Any idea why? Of course I’m pretty new to C++ coding.

Is there any website where i can test POST? Just to see if it catches the content that i send via json.
I’ve been testing GET with this site: https://pokeapi.co

I realize it’s been a while since the question, but when an error of “* to incomplete class type is not allowed” is usually an issue with the file you’re working in (most likely the .cpp if you’re implementing) doesn’t have an include line for the header file where the type is defined. So for this specific issue, you would want to add to the include section of your file


#include "Runtime/Online/HTTP/Public/HttpModule.h"

If someone googles through this thread (it’s the first result) and just wants to copy paste a quick http request without butchering your code with all of that above, here it is:

    auto request = FHttpModule::Get().CreateRequest();
    request->SetURL(FString("http://google.com"));
    request->OnProcessRequestComplete().BindLambda([](FHttpRequestPtr request, FHttpResponsePtr response, bool success) {
    });
    request->ProcessRequest();
7 Likes