Skip to content

Make http request

In this example this template has been made for shotgun rest api request.

cpp

/** template MakeHttpRequest */
template <typename TResponse> 
	TResponse YourModuleName::MakeHttpRequest(const FString& verb, const FString& route, const FString& bearerAuthToken, const FString& acceptHeader, const FString& contentTypeHeader, const FString& bodyContent)
{
	TResponse shotgunResponseContainer;
	auto onRequestComplete = [&](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccessful)
	{
		const FString& strResponse = Response.Get()->GetContentAsString();			
		if (!FJsonObjectConverter::JsonObjectStringToUStruct<TResponse>(strResponse, &shotgunResponseContainer))
		{
			UE_LOG(LogTemp, Error, TEXT("ERROR When execute JsonObjectStringToUStruct on response."));
		}
	};

	FHttpModule& http = FHttpModule::Get();
	auto httpRequest = http.CreateRequest();
	httpRequest->OnProcessRequestComplete().BindLambda(onRequestComplete);
	httpRequest->SetURL(route);
	httpRequest->SetVerb(verb);

	httpRequest->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
	httpRequest->SetHeader(TEXT("Content-Type"), contentTypeHeader);
	httpRequest->SetHeader(TEXT("Accept"), acceptHeader);
	httpRequest->SetHeader(TEXT("Authorization"), FString::Printf(TEXT("Bearer %s"), *bearerAuthToken));

	httpRequest->SetContentAsString(bodyContent);

	if (httpRequest->ProcessRequest())
	{
		// Need this to wait for the end of http request
		http.GetHttpManager().Flush(EHttpFlushReason::FullFlush);
	}

	return shotgunResponseContainer;
}

Async example

cpp
/** template MakeHttpRequest */
template <typename TResponse>
void YourModuleName::MakeHttpRequestAsync(const FString& verb, const FString& route, const FString& bearerAuthToken, const FString& acceptHeader, const FString& contentTypeHeader, const FString& bodyContent, TFunction<void(TResponse)> OnRequestComplete)
{
	auto onRequestComplete = [&, OnRequestComplete](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccessful)
	{
		TResponse shotgunResponseContainer;
		const FString& strResponse = Response.Get()->GetContentAsString();
		if (!FJsonObjectConverter::JsonObjectStringToUStruct<TResponse>(strResponse, &shotgunResponseContainer))
		{
			UE_LOG(LogTemp, Error, TEXT("ERROR When execute JsonObjectStringToUStruct on response."));
		}
		if(OnRequestComplete) OnRequestComplete(shotgunResponseContainer);
	};

	FHttpModule& http = FHttpModule::Get();
	auto httpRequest = http.CreateRequest();
	httpRequest->OnProcessRequestComplete().BindLambda(onRequestComplete);
	httpRequest->SetURL(route);
	httpRequest->SetVerb(verb);

	httpRequest->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
	httpRequest->SetHeader(TEXT("Content-Type"), contentTypeHeader);
	httpRequest->SetHeader(TEXT("Accept"), acceptHeader);
	httpRequest->SetHeader(TEXT("Authorization"), FString::Printf(TEXT("Bearer %s"), *bearerAuthToken));

	httpRequest->SetContentAsString(bodyContent);

	httpRequest->ProcessRequest();
}