Server Sent Error Handling

Godot Version

4.1.1

Question

I am making http-requests to a server using the HTTPRequest. It works good but the problem is in error handling.
For example, I submit a form it has some missing parameters. Server sents a message through error.

Lets say, required fields are {name, password} User Submitted only name
So server sents a response code, also error messages. But that error message never I could find from Godot.


func _http_request_completed(result, response_code, _headers, body):
	        print(data) ## this returns html text
		var json = JSON.parse_string(data)
		print("json format error", json)
		var ToastScene = preload("res://shared/toast/Toast.tscn").instantiate()
		get_tree().root.add_child(ToastScene)
		ToastScene.toast_type = ToastScene.TOAST_TYPE.FAILED
		ToastScene.show_message("Request Error Occured")
		## TODO: SHOW ERROR ON UI
		print("request error with response code: ", response_code)
		return
	var json = JSON.parse_string(data)
	if json:
		request_completed_with_data.emit(json)
	else:
		request_completed_with_data.emit(data)

The backend written using laravel

How do I get that error message that server sent? If I use browser to call api in network tab I can see the error response but in godot I can’t.

The error message should be part of the response body.
Try using print(body) in your method and see if it containts what you’re looking for.

It returns a binary data [10, 123, 34, 114, 101, 113, 117, 101, 115, 116, 95, 116, 121, 112, 101, 34, 58, 34, 114, 101, 113, 117, 105, 114, 101, 100, 34, 58, 49, 44, 34, 109, 97, 120, 95, 108, 101, 110, 103, 116, 104, 34, 58, 53, 48, 44, 34, 100, 101, 102, 97, 117, 108, 116, 95,...]

if I do
var data = body.get_string_from_utf8().strip_edges()
print(data) ## this returns the html