Hello everyone. I’m trying to do http request and response on c++ as blueprintcallable. But I couldn’t handle the delegates. I cannot access HTTP Response after binding OnResponseRecieved function. How can I access my HTTP Response on blueprintcallable. I couldn’t succeed on Multicast delegates.If you think Multicast Delegates is the best option for this. Can you help me with that. here is my codes
MyBlueprintFunctionLibrary.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "Runtime/Core/Public/Delegates/Delegate.h"
#include "MyBlueprintFunctionLibrary.generated.h"
/**
*
*/
UCLASS()
class TESTPLUGIN_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
FString ApiBaseUrl;
UFUNCTION(BlueprintCallable, Category = "HTTP")
static void SetBaseURL(FString BaseURL, FString& ApiBaseUrl);
UFUNCTION(BlueprintCallable, Category = "HTTP")
static void RequestCall(FString ApiBaseUrl, FString Subroute);
static void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
};
MyBlueprintFunctionLibrary.cpp
#include "MyBlueprintFunctionLibrary.h"
#include "Runtime/Json/Public/Json.h"
void UMyBlueprintFunctionLibrary::SetBaseURL(FString BaseURL, FString& ApiBaseUrl) {
ApiBaseUrl = BaseURL;
UE_LOG(LogTemp, Warning, TEXT("BaseURL is worked"));
}
void UMyBlueprintFunctionLibrary::RequestCall(FString ApiBaseUrl, FString Subroute) {
FHttpModule* Http = &FHttpModule::Get();
TSharedRef<IHttpRequest> Request = Http->CreateRequest();
UE_LOG(LogTemp, Warning, TEXT("Request is worked"));
FScriptDelegate Delegate;
Request->OnProcessRequestComplete().BindStatic(UMyBlueprintFunctionLibrary::OnResponseReceived);
Request->SetURL(ApiBaseUrl + Subroute);
Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
Request->SetHeader(TEXT("Accepts"), TEXT("application/json"));
Request->ProcessRequest();
}
/*Assigned function on successfull http call*/
void UMyBlueprintFunctionLibrary::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
UE_LOG(LogTemp, Warning, TEXT("OnResponseReceived function is ready!!"));
//Create a pointer to hold the json serialized data
TSharedPtr<FJsonObject> JsonObject;
//Create a reader pointer to read the json data
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
//Deserialize the json data given Reader and the actual object to deserialize
if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
//Get the value of the json object by field name
int32 recievedInt = JsonObject->GetIntegerField("userId");
//Output it to the engine
UE_LOG(LogTemp, Warning, TEXT("Response is coming xP"));
UE_LOG(LogTemp, Warning, TEXT("Id is: %d"), recievedInt);
}
}