Object 상속 받아 사용
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Http.h"
#include "UObject/NoExportTypes.h"
#include "RestAPI.generated.h"
UCLASS()
class UNREALTEST_API URestAPI : public UObject
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
URestAPI();
FString Post(FString id, FString num);
FString Get();
FString Put(FString id, FString num);
FString Delete();
FString PostResponse;
FString GetResponse;
FString PutResponse;
FString DeleteResponse;
protected:
void ResponseOfPostAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully);
void ResponseOfGetAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully);
void ResponseOfPutAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully);
void ResponseOfDeleteAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "RestAPI.h"
FString URestAPI::Post(FString id, FString num)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Post Request"));
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
TSharedRef<FJsonObject> RequestObj = MakeShared<FJsonObject>();
//RequestObj->SetStringField("id", "cucu154");
RequestObj->SetStringField("id", *id);
RequestObj->SetStringField("num", *num);
FString RequestBody;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&RequestBody);
FJsonSerializer::Serialize(RequestObj, Writer);
Request->OnProcessRequestComplete().BindUObject(this, &URestAPI::ResponseOfPostAPI);
Request->SetURL("<http://localhost:8080/post>");
Request->SetVerb("POST");
Request->SetHeader("Content-Type", "application/json");
Request->SetContentAsString(RequestBody);
Request->ProcessRequest();
return PostResponse;
}
FString URestAPI::Get()
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Get Request"));
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &URestAPI::ResponseOfGetAPI);
Request->SetURL("http://localhost:8080/get");
Request->SetVerb("GET");
Request->ProcessRequest();
return GetResponse;
}
FString URestAPI::Put(FString id, FString num)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Put Request"));
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
TSharedRef<FJsonObject> RequestObj = MakeShared<FJsonObject>();
//RequestObj->SetStringField("id", "cucu154");
RequestObj->SetStringField("id", *id);
RequestObj->SetStringField("num", *num);
FString RequestBody;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&RequestBody);
FJsonSerializer::Serialize(RequestObj, Writer);
Request->OnProcessRequestComplete().BindUObject(this, &URestAPI::ResponseOfPutAPI);
Request->SetURL("<http://localhost:8080/put>");
Request->SetVerb("POST");
Request->SetHeader("Content-Type", "application/json");
Request->SetContentAsString(RequestBody);
Request->ProcessRequest();
return PutResponse;
}
FString URestAPI::Delete()
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Delete Request"));
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &URestAPI::ResponseOfDeleteAPI);
Request->SetURL("http://localhost:8080/delete");
Request->SetVerb("GET");
Request->ProcessRequest();
return DeleteResponse;
}
void URestAPI::ResponseOfPostAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
TSharedPtr<FJsonObject> ResponseObj;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(Reader, ResponseObj);
if (GEngine) {
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *Response->GetContentAsString());
}
PostResponse = *Response->GetContentAsString();
}
void URestAPI::ResponseOfGetAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
TSharedPtr<FJsonObject> ResponseObj;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(Reader, ResponseObj);
if (GEngine) {
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *Response->GetContentAsString());
}
GetResponse = *Response->GetContentAsString();
}
void URestAPI::ResponseOfPutAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
TSharedPtr<FJsonObject> ResponseObj;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(Reader, ResponseObj);
if (GEngine) {
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *Response->GetContentAsString());
}
PutResponse = *Response->GetContentAsString();
}
void URestAPI::ResponseOfDeleteAPI(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
TSharedPtr<FJsonObject> ResponseObj;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(Reader, ResponseObj);
if (GEngine) {
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, *Response->GetContentAsString());
}
DeleteResponse = *Response->GetContentAsString();
}