The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

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?

in Engine by (72 points)
edited by

3 Answers

0 votes

I was having the same problem while creating a check update mechanic, the case with my code was that var datatosend wasn’t a dict, but a string with the json code and in php I needed to use filegetcontents('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;
by (14 points)
0 votes

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

by (236 points)
0 votes

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"]
sslvalidatedomain: 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){
by (26 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.