I have set up a multiplayer dedicated server that runs using PHP scripts and MySQL databases. I am trying to access the server via HTTP to send/receive game data, starting with something as simple as getting the server status.
I have been able to contact the server successfully with this code:
void ANetwork::getContentsOfURL(FString URL)
{
serverResponse = NULL;
TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
HttpRequest->SetURL(URL);
HttpRequest->SetVerb(TEXT("POST"));
//Creating JSON Object
FString json = "{\"auth\":\"" + authenticator = "\"";
json += "}";
HttpRequest->SetContentAsString(json);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &ANetwork::OnResponseReceived);
HttpRequest->ProcessRequest();
}
void ANetwork::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, Response->GetContentAsString());
if (!Response.IsValid())
{
serverResponse = "FAIL";
}
else
{
serverResponse = Response->GetContentAsString();
}
}
And this echoes the proper codes to the debugger, so I know the server is working and the code is in fact getting what it needs to get. However, I need to be able to get the HTTP response as an FString and return it to the caller so that I can use it in-game. Right now this method is asynchronous, which prevents me from returning the response.
How can I make a synchronous HTTP Request so that I can return the response as a string to the caller?
i.e.
FString ANetwork::getContentsOfURL(FString URL)