How to handle streaming HTTP response?

Hello everyone!
I’m trying to connect my Godot project to the Lichess API. Lichess is basically a free chess website/backend. I’m using Godot 4.3 and Windows 11.

I was able to get some of the static content of their API with no problem, so I tried to get real-time data working. As a test, I use this endpoint to get their featured game (They showcase the “most interesting” game that is currently happening on the homepage and also send data through that endpoint. It’s a never ending stream of data).

But I have no idea how to handle this in Godot! I’ve tried using a normal HTTPRequest, but the problem with it is that Godot seems to wait when the response “ends”. Because it never actually ends and just gets fed more and more data, the signal request_completed never actually gets triggered. And I haven’t seen another way of extracting data from the request except using that signal.

I have tried using HTTPClient, but I can’t seem to work out how to extract data from that too. In fact, when trying to get the static content from before which worked fine with an HTTPRequest it doesn’t work anymore.

# I initialise the request
func _ready() -> void:
	http_client = HTTPClient.new()
	http_client.connect_to_host("https://lichess.org")
	# Request the public data of my own profile
	http_client.request(HTTPClient.METHOD_GET, "/api/users/status?ids=ifgiU&withSignal=true", [])
	stream = true


# I poll the result
func _process(delta: float) -> void:
	if stream:
		http_client.poll()
		printt(http_client.get_status(), http_client.has_response())

In the example above what I get is status code 5 for a few seconds, which then turns into 8 (Which is a non-descript error). At no point in this whole ordeal does http_client.has_response() return true.

The problem was solved over on reddit.
Basically, I have to ensure http_client.get_status() returns STATUS_CONNECTED before sending any requests.
This not only made the static API call work, but the real-time API also worked wonderfully with that method.

1 Like

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