godotengine / godotengine/godot-docs

Adding .bind() tip to HttpRequest (and maybe other pages?)

Open
#8,139 0 comments 0 reactions 0 assignees View on GitHub
area:class reference enhancement
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

Description

**Your Godot version:**
4.1.1

**Issue description:**
I was making a Server browser for a game, where the client gets a status update from each server with player count and ping.

But following the documentation, it made it clear that it only returned **response headers** in the **request_completed**.
**Request headers**, as you get from other webclients, wasn't available in the signal. So making a single **request_completed** signal callback that would handle a dynamic amount of **Http Requests** would be impossible without more data.

Luckily I got help on the Godot Engine Discord server, where I was informed that I could use .bind() to add one or more data to the **request_completed** signal connection like this:

```gdscript
func update_server_status():
# servers looks like this: [
# { "id": 1, "name"; "a good testserver 1", "address": "test01.aws.example.org" }
# { "id": 2, "name"; "a good testserver 2", "address": "test02.aws.example.org" }
# { "id": 5, "name"; "a good testserver 5", "address": "test05.aws.example.org" }
# ]

for server in servers:
var http_request := HTTPRequest.new()
add_child(http_request)

http_request.request_completed.connect(self._on_status_http_request_request_completed.bind(server, "just extra data for fun"))
http_request.request("https://" + server.address + "/status")

func _on_status_http_request_request_completed(_result, _response_code, _headers, body, server, extraData):
# "server" and ExtraData are the extra argument passed by the .bind() method.
pass
```

This was really valuable information and I think others can benefit from knowing about this tip as well, maybe as a tip for all built-in signals.

Afterwards I tried reading about .bind() on the Godot docs, but was having a really hard time finding anything useful.
All I found was this: https://docs.godotengine.org/en/stable/classes/class_callable.html#class-callable-method-bind

And even though I now know how to use it, this description would never have put me on that path to use it on the HttpRequest in this way.

**URL to the documentation page (if already existing):**
https://docs.godotengine.org/en/stable/classes/class_httprequest.html

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.