Help with HTTP Requests

Godot Version

` 4.3

Question

I'm trying to acsess an api to add a leaderboard into my game. When I get the data from the api everything works fine. However, when i try to send data using a put request, nothing happens. Can anyone help?

You need to provide a bit more information about your problem, there are multiple things that could be going wrong here I cannot determine the cause just from this. Can you for example provide the code snippet of your request?

1 Like


IDK if you can read that but thats my code

Can you please send it in a formatted string, you can do that by clicking on the </> icon or just creating triple backtics “```”.

It should look like this:

extends HTTPRequest

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	print("Hello, this is formatted")
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

Sending code as screenshots is almost never advised since it cannot be copied, adjusted or even read properly.

1 Like
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _on_button_pressed() -> void:
	var data = {
	"UserName": "Nikhil",
	"Score": "15",
	"id": 1
	} 
	{
	"UserName": "Untitled",
	"Score": "3",
	"id": 2
	}
	{
	"UserName": "Untitled",
	"Score": "6",
	"id": 3
	}

	var query = JSON.stringify(data)
	var headers = ["Content-Type: application/json"]
	$HTTPRequest.request("https://apigenerator.dronahq.com/api/kfzJ11oh/leaderboard/1", headers, HTTPClient.METHOD_PUT, query)
func _on_http_request_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
	print("done")

I tested this code and it works for me, are you sure that

_on_button_pressed()

actually gets called?

Also the default name Godot assigns to the signal “request_completed” is

func _on_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:

so make sure that the signal works.

I did some testing and found that the data gets sent from one curly bracket instead of all of them.

Is there a way to fix this?

I think your “var data” has wrong syntax for a Dictionary,try making it an array of dictionaries or something ,but i could be wrong

Oh I thought that it just was not working you should have said that before, I thought that “nothing happens” meant the HTTP request failed so I just tested that. I see the problem in your code now, you forgot the brackets “”. GDScript can sometimes be too friendly to you and will not crash when doing something like this.

Correct code would be:

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _on_button_pressed() -> void:
	var data = [{ #Here you were missing a bracket
	"UserName": "Nikhil",
	"Score": "15",
	"id": 1
	}, # And of course commas
	{
	"UserName": "Untitled",
	"Score": "3",
	"id": 2
	}, # And of course commas
	{
	"UserName": "Untitled",
	"Score": "6",
	"id": 3
	}] # Here you were also missing a bracket

	var query = JSON.stringify(data)
	var headers = ["Content-Type: application/json"]
	$HTTPRequest.request("https://apigenerator.dronahq.com/api/kfzJ11oh/leaderboard/1", headers, HTTPClient.METHOD_PUT, query)
func _on_http_request_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
	print("done")

They both fixed it. I also had the wrong path. :+1::upside_down_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.