x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

HttpRequest json POST nothing in the php $_POST

I'm trying to send a json content to a php file. The server receives the request but the $_POST array is empty.

The code to send the json data a process the http request:

 bool FOnlineIdentityKadeo::Login(int32 LocalUserNum, const FOnlineAccountCredentials& AccountCredentials)
 {
     UE_LOG(LogNet, Verbose, TEXT("FOnlineIdentityKadeo::Login - LocalUserNum=%i   AccountCredentials.Type=%s  AccountCredentials.Id=%s  AccountCredentials.Token=%s"), LocalUserNum, *AccountCredentials.Type, *AccountCredentials.Id, *AccountCredentials.Token);
     bool bWasSuccessful = false;
     if (!bHasLoginOutstanding && KadeoEndpointProtocol.Len() && KadeoEndpoint.Len() && ServerType > 0 &&  ClientId.Len())
     {
         TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
         // Sets the json data
         JsonObject->SetStringField(TEXT("uemail"), *AccountCredentials.Id);
         JsonObject->SetStringField(TEXT("upassword"), *AccountCredentials.Token);
         JsonObject->SetNumberField(TEXT("servertype"), (double)ServerType);
         JsonObject->SetStringField(TEXT("clientid"), *ClientId);
         FString OutputString;
         TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<>::Create(&OutputString);
         FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);
 
         TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
         HttpRequest->SetVerb(TEXT("POST"));
         HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
         HttpRequest->SetURL(FString::Printf(TEXT("%s://%s"), *KadeoEndpointProtocol, *KadeoEndpoint));
         HttpRequest->SetContentAsString(OutputString);
         HttpRequest->OnProcessRequestComplete().BindRaw(this, &FOnlineIdentityKadeo::OnLoginHttpResponseReceived);
 
         FHttpModule::Get().SetHttpTimeout(300);
 
         if (!HttpRequest->ProcessRequest())
         {
             UE_LOG(LogNet, Error, TEXT("FOnlineIdentityKadeo::Login - cannot process request"));
 
         }
 ...
 }


The vars are

 [OnlineSubsystemKadeo]
 bEnabled=true
 KadeoEndpointProtocol=http
 KadeoEndpoint=192.168.1.107/internal/account/auth/userlogin.php
 ServerType=10
 ClientId=1719301132d7846bc03cea909a8ec8e801bfbf0c5094bbe818855962fe83bf26
 MaxCheckElapsedTime=30;
 

The userlogin.php receives the request, but the $_POST array is empty.

 <?php
 /**
  * @copyright Kadeo 2003-2016
  */
 // It is necessary to sets the content type in the header since
 // the ksgm requester is waiting for.
 header('Content-Type: application/json');
 
 
 require_once './internal/php/inc.all.php';
 
 $email = '';
 if (isset($_POST['uemail']))
     $email = $_POST['uemail'];
     else{
         echo jsonErrorMsg(KERR_INVALIDPARAM);
         exit();
     }
 
 
 $pswd = '';
 if (isset($_POST['upassword']))
     $pswd = $_POST['upassword'];
     else{
         echo jsonErrorMsg(KERR_INVALIDPARAM);
         exit();
     }
 
 $clientId = '';
 if (isset($_POST['clientid']))
     $clientId = $_POST['clientid'];
     else{
         echo jsonErrorMsg(KERR_INVALIDPARAM);
         exit();
     }
 
 $serverType = '';
 if (isset($_POST['servertype']))
     $serverType = $_POST['servertype'];
     else{
         echo jsonErrorMsg(KERR_INVALIDPARAM);
         exit();
     }
 
 // Check the match between the server type and the client id
 if (KAccountManager::CheckClientId($clientId, $serverType) == FALSE){
     echo jsonErrorMsg(KERR_INVALIDCLIENTID);
     exit();
 }
     
 // Check login information
 $token = KAccountManager::CheckLogin($email, $pswd);
 if ($token !== FALSE)
     echo '{"Result" : "Accepted", "AuthToken" : "'.$token.'"}';
     else
         echo jsonErrorMsg(KERR_INVALIDLOGIN);
 ?>


I also received the response from the php.

 void FOnlineIdentityKadeo::OnLoginHttpResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
 {
     if (bWasSuccessful && Response.IsValid() && Response->GetContentType() == TEXT("application/json"))
     {
         //Create a pointer to hold the json serialized data
         TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
 
         //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))
         {
             FString result = JsonObject->GetStringField("Result");
             if (result == TEXT("Accepted"))
             {
                 FString authToken = JsonObject->GetStringField("AuthToken");
 
             }
             else
             if (result == TEXT("Error"))
             {
                 int32 Code = (int32)JsonObject->GetNumberField("Code");
                 FString Message = JsonObject->GetStringField("Message");
                 UE_LOG(LogOnline, Verbose, TEXT("FOnlineIdentityKadeo::OnLoginHttpResponseReceived  -  Request is successful  ErrorCode=%i   ErrorMessage=%s"), Code, *Message);
                 // Handle error
             }
         }
     }
     else
     {
         // Handle error
         UE_LOG(LogOnline, Error, TEXT("FOnlineIdentityKadeo::OnLoginHttpResponseReceived  -  Request return bWasSuccessful=%i  ContentType=%s"), bWasSuccessful, (Response.IsValid()) ? *Response->GetContentType() : TEXT(""));
     }
 }
 


Why the php server does not receive the data in the post ?

Txs for your help.
D.

Product Version: UE 4.12
Tags:
more ▼

asked Sep 16 '16 at 08:11 AM in C++ Programming

avatar image

domzorg
690 47 55 125

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Oct 14 '16 at 06:23 PM

avatar image

domzorg
690 47 55 125

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question