https://algorfati.tistory.com/14 ์ด๊ฑฐ ์ฐธ๊ณ ํ•œ๊ฑด๋ฐ

ATestProjectGameModeBase::ATestProjectGameModeBase(const class FObjectInitializer& ObjectInitializer)

: Super(ObjectInitializer)

{

// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.

PrimaryActorTick.bCanEverTick = true;

Http = &FHttpModule::Get();

}


void ATestProjectGameModeBase::StartPlay()

{

Super::StartPlay();

}


void ATestProjectGameModeBase::HttpCall(const FString& InURL, const FString& InVerb)

{

TSharedRef<IHttpRequest> Request = Http->CreateRequest();

Request->OnProcessRequestComplete().BindUObject(this, &ATestProjectGameModeBase::OnResponseReceived);


//This is the url on which to process the request


Request->SetURL(InURL);

Request->SetVerb(InVerb);

Request->SetHeader("Content-Type", TEXT("application/json"));

TSharedRef<FJsonObject> RequestObj = MakeShared<FJsonObject>();

RequestObj->SetStringField("input", "obj");

RequestObj->SetStringField("voice", "obj");



FString RequestBody;

TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&RequestBody);

FJsonSerializer::Serialize(RequestObj, Writer);


Request->SetContentAsString(RequestBody);


Request->ProcessRequest();


UE_LOG(LogTemp, Warning, TEXT("HttpCall url : %s\nrequestBody : %s"), *InURL, *RequestBody);

}



void ATestProjectGameModeBase::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)

{

UE_LOG(LogTemp, Warning, TEXT("OnResponseReceived url : %s\nrecv : %s"), *Request->GetURL(), *Response->GetContentAsString());


//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("customInt");


UE_LOG(LogTemp, Warning, TEXT("HTTP request result customInt : %d"), recievedInt);

}

}

OnResponseReceived ์—์žˆ๋Š” ๋กœ๊ทธ๋“ค์ด ์ œ๋Œ€๋กœ ์ž‘๋™์„์•ˆํ•จ ์ค‘๋‹จ์ ๊ฑธ์–ด๋ด๋„ ์•„์˜ˆ ์•ˆ์žกํžˆ๊ณ 
๋ฆฌ์Šคํฐ๊ฐ’์„ ๋ฐ›์•„์•ผ๋˜๋Š”๋ฐ ์•ˆ๋˜์„œ ์ด๊ฑฐ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ๋˜๋Š”์ง€ ์•Œ๋ ค์ค„์ˆ˜์žˆ๋‚˜??
POST๋Š” ์ œ๋Œ€๋กœ ์ž‘๋™ํ–ˆ์Œ