Need help to GET Json with Cpp

Greetings, i’m trying to Get some Json data in a local server, but i can’t, i tried some tutorials, my code is exatly the same of the tutorials but nothing happens.


My actual code (same as examples basically) :


// Fill out your copyright notice in the Description page of Project Settings.

#include "HttpActor.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "CPPProject.h"
#include "Engine.h"

// Sets default values
AHttpActor::AHttpActor(const class FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	//When the object is constructed, Get the HTTP module
	Http = &FHttpModule::Get();

// Called when the game starts or when spawned
void AHttpActor::BeginPlay()

/*Http call*/
void AHttpActor::MyHttpCall()
	TSharedRef<IHttpRequest> Request = Http->CreateRequest();
	Request->OnProcessRequestComplete().BindUObject(this, &AHttpActor::OnResponseReceived);
	//This is the url on which to process the request
	Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
	Request->SetHeader("Content-Type", TEXT("application/json"));

/*Assigned function on successfull http call*/
void AHttpActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
	//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(TEXT("customInt"));

		//Output it to the engine
		GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, FString::FromInt(recievedInt));

Please help me :confused:

Sending JSON Data to server

Hi Dustia,

Here is little example on how you create JSON data string:

FString data_to_send = ""; // This will the JSONData you will be sending
FString string_value = "Hello World";
int int_value = 10;
bool boolean_value = true;

TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
JsonObject->SetStringField("key1", string_value );
JsonObject->SetNumberField("key2", int_value );
JsonObject->SetBoolField("key3", boolean_value);

TSharedRef<TJsonWriter<>> json_writer = TJsonWriterFactory<>::Create(&data_to_send );
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), json_writer);

// you can print our the data here to confirm it is write

Following is the code example for Sending the above created data to server. This cod assume you are following this

	loginRequest = HttpRequestModule->CreateRequest();
	loginRequest->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
	loginRequest->SetHeader("Content-Type", TEXT("application/json"));
	loginRequest->SetHeader("Shopper-Token", TEXT(""));
	loginRequest->OnProcessRequestComplete().BindUObject(this, &ASDK::OnLoginRequestResponseReceived);

POST Json Data to server

Hi Dustia,

The following code samples assumes that you have followed the to setup all the dependencies in order to make HTTP POST request.

Creating JSON Data:

FString json_data_to_send = "";

FString string_value = "Hello World";
int int_value = 10;
bool boolean_value = true;

TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
JsonObject->SetStringField("key1", password);
JsonObject->SetNumberField("key2", int_value );
JsonObject->SetBoolField("key3", boolean_value );

TSharedRef<TJsonWriter<>> json_writer = TJsonWriterFactory<>::Create(&json_data_to_send );
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), json_writer);

// Can print the data to check if it is correct

Making HTTP POST Request to server (I am making POST request, so had the code example ready, but doing GET is exactly the same)

FHttpRequestPtr request;
request= HttpRequestModule->CreateRequest();
request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
request->SetHeader("Content-Type", TEXT("application/json"));
request->SetContentAsString(json_data_to_send );
request->OnProcessRequestComplete().BindUObject(this, &AMyActor::OnRequestResponseReceived);

Reading/Parsing JSON Data: (this is the one code you might need to compare to yours)

void AMyActor::OnRequestResponseReceived(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);

		FString string_value_read = JsonObject->GetStringField("key");

                // Read all the data via there respective keys and type (int, string, bool)

Let me know if this works for you.


Thank you ctalystnetwork for the answer, i’m studying the code to understand how you did.

But, i have another question, assuming i want to create a simple mmorpg, is Json the best way to connect my game to database? or i can use a simple TCP/UDP to do it?

Thanks a lot for your help :smiley: