HTTPRequest Post value return response_code 400

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

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(
        "monday"=>"1",
        "tuesday"=>"2",
        "wednesday"=>"3",
        "thursday"=>"4",
        "friday"=>"5",
        "saturday"=>"6",
        "sunday"=>"7",
    );

    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]
:bust_in_silhouette: Reply From: Freeman

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 ?

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

pengyou | 2020-03-26 12:46

:bust_in_silhouette: Reply From: pengyou

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.