I am using the HTTP module via C++ to connect to an external API. I already have it hooked up for the GET and POST requests I am making. However, there is a call I need to make that uses the DELETE verb and it looks like that verb is not supported. Does anyone know of a way to send an HTTP delete request?
Sources indicating DELETE is not available: IHttpRequest::SetVerb | Unreal Engine 5.2 Documentation and GitHub - nsjames/UE4_Tutorial_Http: Proper usage of HTTP within UE4
I am using UE 5.3
3dRaven
(3dRaven)
November 29, 2023, 2:11pm
2
Strange because the method is just a string parameter in SetVerb. It should just pass on the variable.
You could try varest as an alternative
in VaRestTypes.h
in it’s verbs you can clearly see DEL
UENUM(BlueprintType)
enum class EVaRestRequestVerb : uint8
{
GET,
POST,
PUT,
DEL UMETA(DisplayName = "DELETE"),
/** Set CUSTOM verb by SetCustomVerb() function */
CUSTOM
};
Just tested
FString UrlAddressAsString = "http://httpbin.org/delete";
FString OutputString = "";
void AHTTPRequestActor::Request(FString UrlAddressAsString, FString OutputString) {
TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetVerb("Delete");
HttpRequest->SetHeader("Content-Type", "application/json");
HttpRequest->SetURL(*FString::Printf(TEXT("%s"), *UrlAddressAsString));
HttpRequest->SetContentAsString(OutputString);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AHTTPRequestActor::OnGetUsersResponse);
HttpRequest->ProcessRequest();
}
void AHTTPRequestActor::OnGetUsersResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(JsonReader, JsonObject);
}
}
Get a normal response with success = true and json return.
Thank you for the response. If you are seeing a success there then I must be having another issue that I thought was being caused by the DELETE verb.
Thanks again
3dRaven
(3dRaven)
November 29, 2023, 2:29pm
4
Using ue5 but I’ll do a ue4 test in a moment.
Edit: revision for UE4 with working delete
header
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Interfaces/IHttpRequest.h"
#include "HTTPRequestActor.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FHttpRequestFinished, bool, Finished, FString, Message);
/**
*
*/
/** Verb (GET, PUT, POST) used by the request */
UENUM(BlueprintType)
enum class EVerb : uint8
{
GET UMETA(DisplayName = "GET"),
POST UMETA(DisplayName = "POST"),
PUT UMETA(DisplayName = "PUT"),
DELETE UMETA(DisplayName = "DELETE"),
};
UCLASS(Blueprintable)
class HTTPREQ_API AHTTPRequestActor : public AActor
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void Request(FString Url, EVerb Verb,FString PassedContent);
void OnGetUsersResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
UPROPERTY(BlueprintAssignable)
FHttpRequestFinished RequestFinishedDelegate;
};
cpp
#include "HTTPRequestActor.h"
#include "HttpModule.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Interfaces/IHttpResponse.h"
void AHTTPRequestActor::Request(FString Url, EVerb Verb, FString PassedContent)
{
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EVerb"), true);
FString VerbName = EnumPtr->GetDisplayNameText(static_cast<int>(Verb)).ToString();
HttpRequest->SetVerb(VerbName);
HttpRequest->SetHeader("Content-Type", "application/json");
HttpRequest->SetURL(*FString::Printf(TEXT("%s"), *Url));
HttpRequest->SetContentAsString(PassedContent);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AHTTPRequestActor::OnGetUsersResponse);
HttpRequest->ProcessRequest();
}
void AHTTPRequestActor::OnGetUsersResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
{
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(JsonReader, JsonObject);
// example parsing
/*
FString string_value_read = JsonObject->GetStringField("success");
if (string_value_read == "true") {
RequestFinishedDelegate.Broadcast(true, "Success");
}
else {
RequestFinishedDelegate.Broadcast(false, "Key not found response: " + Response.Get()->GetContentAsString());
// Request failed
}
*/
}
else {
FString Message = "Request failed [" + FString::FromInt(Response.Get()->GetResponseCode()) + "] " + Response.Get()->GetContentAsString();
RequestFinishedDelegate.Broadcast(false, Message);
}
}
system
(system)
Closed
December 29, 2023, 2:30pm
5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.