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๋ ์ ๋๋ก ์๋ํ์
๋๊ธ 0