Download

Crash course with ue4 and c++, custom blueprint node with onsuccess/onfail

Up to this point I have figured out how to expose some functions to blueprint from code and have sent an http request to the server and received a response. Now I need some help combining these 2 methods so I can have a nice custom blueprint node that fires when the HTTP request is complete. So basically I don’t know how to create the Exec pins onSuccess and onFailure for my custom blueprint node. I also don’t know how I would incorporate those Exec pins to this code that makes the request:



void CustomBP::StartRequest(FString method, FString JSONPostData)
{
	TSharedRef <IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
	HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
	HttpRequest->SetURL(TEXT("http://127.0.0.1:3000"+ method));
	HttpRequest->SetVerb(TEXT("GET"));
	//HttpRequest->SetContentAsString(TEXT(JSONPostData));
	HttpRequest->OnProcessRequestComplete().BindUObject(this, &CustomBP::OnRequestComplete);

	if (!HttpRequest->ProcessRequest())
	{
		GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Green, TEXT("Request failed at start!"));
	}
}

void CustomBP::OnRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
	FString MessageBody = "";
	if (!Response.IsValid())
	{
		MessageBody = "{\"success\":\"Error: Unable to process HTTP Request!\"}";
		GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Green, TEXT("Request failed!"));
	}
	else if (EHttpResponseCodes::IsOk(Response->GetResponseCode()))
	{
		MessageBody = Response->GetContentAsString();
		GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Green, TEXT("Request success! "+ MessageBody));
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Green, TEXT("Request error!"));
		MessageBody = FString::Printf(TEXT("{\"success\":\"HTTP Error: %d\"}"), Response->GetResponseCode());
	}
}