0 votes

I try to follow godot documentation and implement function _ make_ post_ request. But result only shows null with response code returned 400. Anyone please help me.

My godot script:

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",true)

php script:

public function actionTest(){
$day = array(

    echo json_encode($day);     

my return headers:

[Date: Wed, 25 Mar 2020 15:34:41 GMT, Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.11, X-Powered-By: PHP/7.3.11, Access-Control-Allow-Origin: http://mall.buymall.com.my, Set-Cookie: PHPSESSID=cldhaop58v2ajo0ebn6hq3b2r0; path=/, Expires: Thu, 19 Nov 1981 08:52:00 GMT, Cache-Control: no-store, no-cache, must-revalidate, Pragma: no-cache, Set-Cookie: current=2ba250beefcbd28cca9514c71681cfeb8a9e6ab5s%3A3%3A%22web%22%3B; expires=Fri, 24-Apr-2020 15:34:41 GMT; Max-Age=2592000; path=/, Set-Cookie: YII_CSRF_TOKEN=54c897c15fba9d2ad4fad12ce7219d69f478a744s%3A88%3A%22NmlPMnJxZ1ZjRG1vOFZCcHdPejBuN01mNHRJRFk0UloxSdwtq6pnFwQEwKV1FgfENsT7rNs5xG6UwQMyVzs9Dw%3D%3D%22%3B; path=/, Connection: close, Transfer-Encoding: chunked, Content-Type: text/html; charset=UTF-8]
asked Mar 25 in Engine by pengyou (42 points)
edited Mar 25 by pengyou

2 Answers

0 votes
Best answer

The reason is due to CSRF validation. Once i turn off the csrf validation at my site, the problems solved. But now i have another issue that the data i post to my site is always null.

answered Mar 27 by pengyou (42 points)
selected Mar 27 by pengyou
0 votes

It looks like the server is not processing the request due to client error - something like wrong request syntax etc.. It's exactly the same situation when you put wrong webpage address in your browser and see typical "400 Bad Request" message.

I am not an expert, but aybe, because there is no
func _on_HTTPRequest_request_completed ?

answered Mar 25 by Freeman (707 points)

Thank you for reading my question. I did have write the function onHTTPRequestrequestcompleted. And $HTTPRequest.request(url) on the same url did works for me.

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 How to use this Q&A? before posting your first questions.