Hi. I have setup my project so that it sends post data to a website (php script). This is working fine. But, I want to receive a string back from the website. In my php, I am doing echo of the string I want to send back. In my project, this is my setup:
bool AHTTPHandlerActor::TransmitRequest()
{
FSagRequest CurrentRequest;
FString Dest;
Dest.Equals("XYZ.php");
CurrentRequest.SetDestination(Dest);
TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
Request->SetHeader("Content-Type", "application/x-www-form-urlencoded");
Request->SetURL(TEXT("http://ABC.com/XYZ.php"));
Request->SetVerb(TEXT("POST"));
FString NumberOfLevelsString(TEXT("NumberOfLevels"));
FString NumberOfLevelsStringValue = FString::FromInt(GlobalArrayLength);
CurrentRequest.AddURLPair(NumberOfLevelsString, NumberOfLevelsStringValue); //This internally makes the // //string to send. This works, because I am successfully sending data to the website.
Request->SetContentAsString(CurrentRequest.TheData);
Request->OnProcessRequestComplete().BindUObject(this, &AHTTPHandlerActor::OnResponseReceived);
if (!Request->ProcessRequest())
{
return false;
}
delete LevelData;
return true;
}
Here is the responseReceived function:
void AHTTPHandlerActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
FString MessageBody = "";
// If HTTP fails client-side, this will still be called but with a NULL shared pointer!
if (!Response.IsValid())
{
MessageBody = "{\"success\":\"Error: Unable to process HTTP Request!\"}";
}
else if (EHttpResponseCodes::IsOk(Response->GetResponseCode()))
{
MessageBody = Response->GetContentAsString();
}
else
{
MessageBody = FString::Printf(TEXT("{\"success\":\"HTTP Error: %d\"}"), Response->GetResponseCode());
}
ResponseStr = MessageBody;
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, ResponseStr);
//Closed(MessageBody);
}
I am getting the response string as NULL. Does anyone know the issue?