[UE 4.10] How to perform http requests properly

Hello everyone,

I’m trying to connect my game to a WebAPI, sending POST messages to url via HTTP protocol but apparently none method works. I googled for hours finding solution, but it seems like none of the solutions work. What I want to do:

  • Hosted WebAPI server on a domain server.mydomain.com (just example)
  • On the server there is controller which has a method that returns value 100 as a string.
  • I want using either GET or POST get that value from the server and apply it on an Actor in the game.

What I tried:

  • Rama’s solution - it seems like some methods from the FHttpModule has been modified since and doesn’t seem to work.
  • Created own library in C++ with CLI support from C# classes to communicate with WebAPI - didn’t work either
  • Downloaded plugins by other users, which didn’t work either
  • Thought that WebAPI might be the problem, so created a php scripts to do the same job, didn’t work. (I’m sure the php code is proper because it worked on another app I used)

I wonder what am I missing. Can somebody help please? I don’t know how to use the FHttpModule myself.

A snippet from my Facebook plugin, of course with some minor changes. There’s a lot more to it than this, but I hope this can point you in the right direction.

.h file:
FDelegateHandle SubscribeUser(FUseDelegateBool& UseDelegate);
FUseEvent MyEvent;

.cpp file:
UMyHTTPClass::AnotherClass{

FDelegateHandle UMyHTTPClass::SubscribeUser(FUseDelegate& UseDelegate) {
return FUseEvent .Add(UseDelegate);
}

void UMyHTTPClass::MyHttpCall() {
TSharedRef< IHttpRequest > Request = Http->CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &UMyHTTPClass::OnResponseReceived);
Request->SetURL(“http://www.myDomain.com”);
Request->SetVerb(“POST”);
Request->SetHeader(TEXT(“User-Agent”), “X-UnrealEngine-Agent”);
Request->SetHeader(TEXT(“Host”), “http://www.myDomain.com”);
Request->SetHeader(“Content-Type”, TEXT(“application/json”)); //or “application/x-www-form-urlencoded”
if (!Request->ProcessRequest()) {
MyEvent.Broadcast(“Internal error: ProcessRequest failed”, false);
}
}

void USNFacebookHTTP::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {
if (EHttpResponseCodes::IsOk(Response->GetResponseCode())) {
MyEvent.Broadcast(Response->GetContentAsString(), bWasSuccessful);
} else {
MyEvent.Broadcast(Response->GetContentAsString(), false);
}
}