HTTP data format issues

Hi. I am tring to setup a basic HTTP connection between my game and localhost. The connection is working fine. But, I am receiving null at the database.

Here is the code chunk being used:

TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
Request->SetURL(TEXT("http://127.0.0.1/insertGameStats.php"));
Request->SetVerb(TEXT("POST"));
Request->SetContentAsString(CurrentRequest.TheData);

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

Here is how I set data:

  CurrentRequest.AddURLPair(param, value);
  void AddURLPair(FString param, FString value)
{
	TheData += (delim + param + "=" + value);
	delim = "&";
}

The php Script:

$con = mysql_connect($db_hostname,$db_username);

if(!$con)
{
die("Unable to Connect to MySQL: " . mysql_error());
}

$val = $_POST[‘val’];

mysql_select_db($db_database, $con);

$sql = “INSERT INTO testtable(value)
VALUES
(’$val’)”;

if(!mysql_query($sql, $con))
{
die('Error: ’ . mysql_error());
}

echo “1 record added to the game stats database”;

mysql_close($con);

I know that the connection is being established, because a new entry is being made in the table. But, it is all NULL.

Any help would be greatly appreciated

I am facing similar issues setting up the php script to receive the data. I keep receiving NULL at the php side. Please help.

I have been trying to setup a way to get some gameplay stats to a server(for analysis), but I’ve been running into similar issues as well. The request does reach my localhost, but not the data. My database has a bunch of rows with just the default value, and not the value I want to send. Also, I have a similar code, but I’m accessing it through blueprints. I need help!

I meet this problem,too . Have you solved this problme? can you help me?my code below.

#include “hzl.h”
#include “Myphpconnection.h”

AMyphpconnection::AMyphpconnection(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP) { FString PostData = FString(TEXT(“{"Tname":”)) + FString(TEXT(“"4446545646544"”)) + TEXT(“}”);

TSharedRef HttpRequest = FHttpModule::Get().CreateRequest();
//HttpRequest->SetHeader(TEXT(“Content-Type”), TEXT(“application/x-www-form-urlencoded”));
HttpRequest->SetHeader(TEXT(“Content-Type”), TEXT(“application/json”));
HttpRequest->SetVerb(“POST”);
HttpRequest->SetURL(TEXT(“http://localhost/index.php”));

HttpRequest->SetContentAsString(PostData);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AMyphpconnection::OnUpdateRequestComplete);
// HttpRequest->ProcessRequest();

if (!HttpRequest->ProcessRequest())
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 500.f, FColor::Yellow, FString::Printf(TEXT(“%s”), *PostData));
GEngine->AddOnScreenDebugMessage(1, 600.0f, FColor::Green, TEXT(“Request failed at start!”));
}
}
else
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 500.f, FColor::Yellow, FString::Printf(TEXT(“%s”), *PostData));
GEngine->AddOnScreenDebugMessage(1, 600.0f, FColor::Green, TEXT(“Request successd at start!”));
}
}/**/

}

void AMyphpconnection::OnUpdateRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {

}

Hi abhishek17, would be great to have a little more info on that.

What is the exact string of CurrentRequest.TheData?
Also, could you please print what is arriving at your PHP script by printing all POST variables using print_r($_POST); in PHP.

I see that your are trying to transfer your data as Content-Tyoe application/json, but you are formatting your data as Content-Type application/x-www-form-urlencoded

Same problem. not data in $_POST.
Someone found a solution ?

I’m not super experienced in this, but it looks like you set your Content-Type to application/json format, but you’re sending the actual content in the format of application/x-www-form-urlencoded

Even with the Request->SetHeader(TEXT(“Content-Type”), TEXT(“application/json”));
Nothing appeared in the $_POST on the php side.

No one found the reason ?

I’m assuming your latest comment was suppose to be a reply to this one. You may have misread my answer. I was saying that your header is already set to application/json, but you’re sending your content in a different format (x-www-form-urlencoded). Change your content to be written in JSON format and it may work.

Responded to this in my answer, since I’m assuming you meant to actually respond to it.

In fact the problem is on the PHP.
The json data sent to the php page is not in $_POST

But you have to get it using

$json = file_get_contents(‘php://input’);
$obj = json_decode($json);

And you have an object with all your values.