HTTPRequest send data to php website always is null at php web

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pengyou

I have been follow godot documentation to send data to my server. The connection is okay, but the data sent to my php web is always null. Anyone please guides me on this.

my godot codes:

func _make_post_request(url, data_to_send, use_ssl):
   var query = JSON.print(data_to_send)
   var headers = ["Content-Type: application/json"]
   $HTTPRequest.request(url, headers, use_ssl, HTTPClient.METHOD_POST, query)

func _on_Button_pressed():
    _make_post_request("http://localhost/buymall/site/test","Hello World",false)

func _on_HTTPRequest_request_completed(result, response_code, headers, body):
    var json = JSON.parse(body.get_string_from_utf8())

my php codes:

public function actiontest()
    $value = array(
    if(isset($_POST) && $_POST!=null)
        echo json_encode($value);
        echo "no post value";

The outcome when i press POST button is always return “no post value” from my php site. What are the things i did wrong?

:bust_in_silhouette: Reply From: magniip

I was having the same problem while creating a check update mechanic, the case with my code was that var data_to_send wasn’t a dict, but a string with the json code and in php I needed to use file_get_contents(‘php://input’) to get the json data instead of using $_POST

Here’s the corrected code that works

Gdscript (Godot 3.3.2):

const VERSION= 10
const USE_SSL=true

var data_to_send = {"version": VERSION,"platform": OS.get_name()}
var query = JSON.print(data_to_send)
var headers = ["Content-Type: application/json"]
httpRequest.request(CHECK_UPDATE_URL, headers, ViewConstants.USE_SSL, HTTPClient.METHOD_POST, query)

In php (works in 7.4) I used the following code to get the 2 variables:

$jsonInput = file_get_contents('php://input');
$dataInput = json_decode($jsonInput);
if (is_object($dataInput)){
    $versionInput = $dataInput->version;
    $platformInput = $dataInput->platform;
:bust_in_silhouette: Reply From: SF123

Try this:


var Req = $HTTPRequest
var data = "Hello World"
func _do_stuff():
    Req.request("http://localhost/buymall/site/test?data=" + data.percent_encode(), [], false)



if (isset($_GET['data'])) {
    echo "Data Sent";
} else {
    echo "No data sent";


But of course, this is only how to do a GET request, sorry

:bust_in_silhouette: Reply From: EquineRecline

Don’t worry, I had to play around with HTTPRequest for a while to get it to send JSON. Maybe it’s not the best way, but it’s what works for me.

The request arguments for me are:

url: Just the URL as normal
custom_headers: [“Content-Type: application/x-www-form-urlencoded”]
ssl_validate_domain: false
method: HTTPClient.METHOD_POST

Then for request_data:
Start with a normal dictionary for your key/value pairs.

var data_dict = {"request":"post", "waiting for":"godot" }

Convert that to JSON.

var data_json = to_json(data_dict)

Make the JSON string the value of a key.

data_json = "data=" + data_json

Send the request

  ["Content-Type: application/x-www-form-urlencoded"],
  'data={"request":"post", "waiting for":"godot"}'

To get the data with PHP:

Get the value of the “data” key (which is the JSON string) and convert it to an associative array.

$request_json = $_POST['data'];
$request_array = json_decode($request_json, true);

Now you should be able to access the values. For example, you could iterate over the array.

for($request_array as $key => $value){