Reputation: 19
I am trying to send nickname to database using MySQL and HTTPRequest in Godot but I'm getting errors.
I don't know what to do because whatever I did i'm still getting errors, used a little help of chatgpt but he is spinning 3 codes all the time.
This is my first time trying to make a fps game for me and my friends. Any help, comment and criticism are welcome. Forgot to mention, I can access server_url
from my browser.
extends Node
@export var line_edit : LineEdit
@export var button : Button
@export var http_request : HTTPRequest
var server_url : String = "http://192.168.0.30/KanarinGame/sacuvaj_nickname.php"
func _on_Button_pressed() -> void:
var nickname : String = line_edit.text
if nickname != "":
print("Poslati nickname: " + nickname)
send_nickname_to_server(nickname)
else:
print("Nickaname nije unet!")
func send_nickname_to_server(nickname: String) -> void:
var body : String = "nickname=" + nickname
var headers : PackedStringArray = PackedStringArray()
headers.append("Content-Type: application/x-www-form-urlencoded")
var body_data : PackedStringArray = PackedStringArray()
body_data.append(body.to_utf8_buffer())
var result = http_request.request(server_url, body_data, headers, HTTPRequest.METHOD_POST)
if result != OK:
print("Greška pri slanju zahteva!")
else:
print("Zahtev poslat uspešno!")
func _on_HTTPRequest_request_completed(result : int, response_code : int, headers : Array, body : PackedByteArray) -> void:
if response_code == 200:
print("Uspešno poslato!")
else:
print("Greška pri slanju: " + str(response_code))
Errors:
res://nickname_screen.gd:30 - Parse Error: Invalid argument for "append()" function: argument 1 should be "String" but is "PackedByteArray".
res://nickname_screen.gd:33 - Parse Error: Cannot find member "METHOD_POST" in base "HTTPRequest".
res://nickname_screen.gd:33 - Parse Error: Invalid argument for "request()" function: argument 3 should be "HTTPClient.Method" but is "PackedStringArray".
Line 30:
body_data.append(body.to_utf8_buffer())
Line 33:
var result = http_request.request(server_url, body_data, headers, HTTPRequest.METHOD_POST)
sacuvaj_nickname.php
:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "game_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nickname = $_POST["nickname"];
$stmt = $conn->prepare("INSERT INTO players (nickname) VALUES (?)");
$stmt->bind_param("s", $nickname);
$stmt->execute();
echo "Nickname je sačuvan!";
$stmt->close();
} else {
echo "Nevalidan zahtev!";
}
$conn->close();
?>
Upvotes: 0
Views: 58
Reputation: 1336
I mean… have you looked at the docs? https://docs.godotengine.org/en/stable/tutorials/networking/http_request_class.html
They show you exactly what to do. Here’s a breakdown of the error messages:
You’re trying to add bytes to a string array. None of this is necessary, because the request body should just be a single string.
METHOD_POST
is a constant of HTTPClient
, not HTTPRequest
.
The order of your arguments is wrong. It should be:
PackedStringArray
, defaults to empty)HTTPClient.METHOD_POST
, defaults to GET)And here’s your fixed method:
func send_nickname_to_server(nickname: String) -> void:
# this is fine for the body, no need for the body_data stuff
var body : String = "nickname=" + nickname
# this was already fine, but you can use this shorthand (you don’t
# even technically need to specify the type but I’m leaving it in):
var headers : PackedStringArray = ["Content-Type: application/x-www-form-urlencoded"]
var result = http_request.request(server_url, headers, HTTPClient.METHOD_POST, body)
if result != OK:
print("Greška pri slanju zahteva!")
else:
print("Zahtev poslat uspešno!")
You may be confusing request()
and request_raw()
, the latter of which does expect the request body as a PackedByteArray
. This is mainly for sending binary data such as images.
Upvotes: 1