Issue with HTTPS Request in Godot – Server Redirects but No Response

Godot Version

4.3

Question

Hi,

I’m implementing an authentication system using HTTPS requests in Godot, but I’ve encountered an issue with server redirection. Godot’s HTTPS client doesn’t seem to follow redirects automatically.

The request reaches the server successfully, and everything works when tested in a browser. However, in Godot, the response status is always 0, and I don’t receive any data back.
I tried using HTTPSRequest node

extends Node

@onready var email_input: LineEdit = $"../Email"
@onready var password_input: LineEdit = $"../Password"
@onready var button: Button = $"../Button"
@onready var http_request: HTTPRequest = $"../HTTPRequest"
@onready var button_2: Button = $"../Button2"

const API_URL = "  "

var request_type = ""

func _ready():
	pass
# Called when sign-up button is clicked
func on_signup_pressed():
	request_type = "signup"
	_send_request("signup")

# Called when login button is clicked
func on_login_pressed():
	request_type = "login"
	_send_request("login")

# Sends a POST request with user data
func _send_request(action: String):
	var email = email_input.text.strip_edges()
	var password = password_input.text.strip_edges()
	
	if email.is_empty() or password.is_empty():
		print("Email and password are required!")
		return
	
	var data = {
		"action": action,
		"email": email,
		"password": password
	}
	
	var json_data = JSON.stringify(data)
	var headers = ["Content-Type: application/x-www-form-urlencoded"]
	
	var error = http_request.request(API_URL, headers, HTTPClient.METHOD_POST, json_data)
	print(error)
	if error != OK:
		print("Error sending request:", error)

# Handles the API response
func _on_request_completed(result, response_code, headers, body):
	var response_text = body.get_string_from_utf8()

	print("headers:", headers)

	
	if response_code == 200:
		if request_type == "signup":
			print("Sign-up response:", response_text)  # Display API message
		elif request_type == "login":
			if response_text == "Login successful":
				print("Login successful! Emitting signal.")
			else:
				print("Login failed:", response_text)
	else:
		print("Request failed with code:", response_code)

And I tried using HTTPSClient

extends Node


var url = "  "

func _ready():
	send_request("signup","pham150603@gmail.com","123")

func send_request(action: String, email: String, password: String):
	var client = HTTPClient.new()
	var tls_options = TLSOptions.client()
	var domain = "script.google.com"
	var endpoint = url.replace("https://" + domain, "")

	print("\n[INFO] Connecting to:", domain)

	var error = client.connect_to_host(domain, 443, tls_options)
	print("[INFO] Connection attempt result:", error)

	while client.get_status() in [HTTPClient.STATUS_CONNECTING, HTTPClient.STATUS_RESOLVING]:
		client.poll()
		await get_tree().process_frame

	print("[INFO] Client status after connection:", client.get_status())

	if client.get_status() != HTTPClient.STATUS_CONNECTED:
		print("[ERROR] Connection failed! Status:", client.get_status())
		return

	var payload = {"action": action, "email": email, "password": password}
	var json_data = JSON.stringify(payload)

	var headers = [
		"Content-Type: application/json",
		"Content-Length: " + str(json_data.length())
	]

	print("\n[INFO] Sending request:")
	print("[DEBUG] Endpoint:", endpoint)
	print("[DEBUG] Headers:", headers)
	print("[DEBUG] Payload:", json_data)

	error = client.request(HTTPClient.METHOD_POST, endpoint, headers, json_data)
	print("[INFO] Request sent, result:", error)

	if error != OK:
		print("[ERROR] Request failed with code:", error)
		return

	# 🌟 WAIT FOR RESPONSE 🌟
	while client.get_status() in [HTTPClient.STATUS_REQUESTING, HTTPClient.STATUS_BODY]:
		print("[INFO] Waiting for response...")  # Debugging log
		client.poll()
		await get_tree().process_frame

	# 🔍 CHECK RESPONSE STATUS
	print("\n[INFO] Response received. Status:", client.get_status())

	# 🔍 READ RESPONSE HEADERS
	var response_headers = client.get_response_headers()
	print("[INFO] Response Headers:")
	for h in response_headers:
		print("[HEADER] ", h)

	# 🔍 READ RESPONSE CODE
	var response_code = client.get_response_code()
	print("[INFO] Response Code:", response_code)

	# 🔍 READ RESPONSE BODY
	var response = PackedByteArray()
	print("[INFO] Reading response body...")

	while true:
		var chunk = client.read_response_body_chunk()
		if chunk.is_empty():
			break  # No more data
		response.append_array(chunk)

	var response_text = response.get_string_from_utf8()
	print("\n[INFO] Raw Response Data:", response)

	if response_text.is_empty():
		print("[ERROR] Response body is empty!")
	else:
		print("\n[SUCCESS] Server Response:\n", response_text)

	return response_text

But I still don’t receive any response, not even a 302 status code or the redirect location to perform a GET request and retrieve the response.
you can test the url and manage the account here
https://kenpham1506.github.io/PI/hello/‮olleh/index.html

In your first example you are telling the server that the Content-Type is application/x-www-form-urlencoded but you are sending a json which is not correct.

Here’s how to encode the info as a application/x-www-form-urlencoded

HTTPRequest should handle redirects automatically but it has a HTTPRequest.max_redirects property you may need to modify.

HTTPClient does not handle redirects automatically and you should handle them manually.

1 Like

thanks