I am trying to call google api in my code. I have a “new.json” file which I am calling and I am trying to POST it but I am not getting the result I want.
Json File = {
“config”: {
“encoding”: “LINEAR16”,
“languageCode”: “en-US”,
“sampleRateHertz”: 16000
},
“audio”: {
“content”: “I have this content ignore this line”
},
}
My Code
void ASaurabh::HttpCall()
{
TSharedRef<IHttpRequest> Request = Http->CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &ASaurabh::OnResponseeReceived);
//This is the url on which to process the request
Request->SetURL("https://speech.googleapis.com/v1/speech:recognize?key=");
Request->SetVerb("POST");
Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
Request->SetHeader("Content-Type", TEXT("application/json"));
Request->ProcessRequest();
}
// Called when the game starts or when spawned
void ASaurabh::BeginPlay()
{
Super::BeginPlay();
HttpCall();
}
void ASaurabh::OnResponseeReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
FString aFullPath = FPaths::GameSavedDir();
aFullPath += "new.json";
FString JsonStr;
FFileHelper::LoadFileToString(JsonStr, *aFullPath);
//if (GEngine) {
//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, JsonStr);
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonStr);
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
if (FJsonSerializer::Deserialize(JsonReader, JsonObject)) {
FString recievedString = JsonObject->GetStringField("audio");
GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, recievedString);
}
}
// Called every frame
void ASaurabh::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}