0
\$\begingroup\$

I've some trouble for sending a post request to my nodejs server.

I can receive data from the server with GET method, but when I try to send data with POST, I've an empty bracket on return on this function:

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

And empty bracket on my JSON console.log on the server too.

My code:

extends CanvasLayer

func _ready():
    $HTTPRequest.connect("request_completed", self, "_on_request_completed")

func _on_Button_pressed():
    var data = {"name": "Godette"}
    _make_post_request("http://localhost:8080/", data, false)

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

func _on_HTTPRequest_request_completed():
    pass

func _make_post_request(url, data_to_send, use_ssl):
    # Convert data to json string:
    var query = JSON.print(data_to_send)
    # Add 'Content-Type' header:
    var headers = ["Content-Type: application/json"]
    $HTTPRequest.request(url, headers, false, HTTPClient.METHOD_POST, query)

And this code return this in my console:

{}
200

Thanks for help :)

\$\endgroup\$
5
  • \$\begingroup\$ What is your server supposed to return? \$\endgroup\$
    – user35344
    Nov 7, 2020 at 10:09
  • \$\begingroup\$ Quick sanity check... Your server isn't expecting a CSRF token or other seurity measures with POSTs is it? \$\endgroup\$
    – Basic
    Nov 7, 2020 at 12:29
  • \$\begingroup\$ Thanks for replies! My server is a basic node server, who console log the JSON received. But on my Godot script the function _on_request_completed must be print the sent json object on the console . But the json object seems empty... for my local test, the server has no security. \$\endgroup\$
    – Akazan
    Nov 7, 2020 at 13:58
  • \$\begingroup\$ JSON.parse returns a JSONParseResult so is json_result.error == OK? \$\endgroup\$ Nov 8, 2020 at 9:54
  • 1
    \$\begingroup\$ Can you show your nodejs code? I'm sure the problem is there. \$\endgroup\$ Nov 25, 2020 at 18:59

1 Answer 1

1
\$\begingroup\$

I find nothing wrong with your code. I suppose the wrong code might be your server code. My GDScript code as you can see is similar to yours:

extends Control

func _ready():
    $HTTPRequest.connect("request_completed", self, "_on_request_completed")

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

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_Send_pressed():
    _make_post_request("http://127.0.0.1:3200/hello/",
        {"name":"godot"}, false)

My server code looks like this:

const express = require('express');

const app = express();
const bodyparser = require('body-parser');

const port = process.env.PORT || 3200;

// middleware

app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: false }));

app.post('/hello', (req, res) => {
  const resp = req.body;

  console.log(resp.name);

  res.status(200).json({
     name: resp.name,
     message: 'hello stranger',
  });
});

app.listen(port, () => {
  console.log(`running at port ${port}`);
});

And when I click on the Send Button I get this output:
{message:hello stranger, name:godot}

\$\endgroup\$
2
  • \$\begingroup\$ Thanks Timothy, I don’t know why but this code run fine! \$\endgroup\$
    – Akazan
    Nov 14, 2020 at 15:01
  • \$\begingroup\$ Glad it worked. Without seeing your server side code there isn't much I can say. I can only speculate that the problem might be in your app.post(...). While answering I used this blog post on alligator as reference, hope it helps. \$\endgroup\$ Nov 15, 2020 at 4:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .