|
| 1 | +class_name HTTPResult |
| 2 | +extends RefCounted |
| 3 | +## A dataclass returned by [method AwaitableHTTPRequest.async_request]. |
| 4 | + |
| 5 | +var _error: Error ## Contains the [method HTTPRequest.request] error, [constant Error.OK] otherwise. See also [method success].[br](For advanced use-cases) |
| 6 | +var _result: HTTPRequest.Result ## Contains the [annotation HTTPRequest] error, [constant HTTPRequest.RESULT_SUCCESS] otherwise. See also [method success].[br](For advanced use-cases) |
| 7 | +var status: int ## The response status code. |
| 8 | +var headers: Dictionary ## The response headers. |
| 9 | +var bytes: PackedByteArray ## The response body as a [PackedByteArray].[br][b]Note:[/b] Any [Array] is always passed by reference. |
| 10 | + |
| 11 | +## Checks whether the HTTP request succeeded, meaning [member _error] and [member _result] aren't in an error state.[br] |
| 12 | +## [b]Note:[/b] This does not check the response [member status] code. |
| 13 | +func success() -> bool: |
| 14 | + return _error == OK and _result == HTTPRequest.RESULT_SUCCESS |
| 15 | + |
| 16 | +## Checks whether the [member status] is between 200 and 299 (inclusive), see [url]https://developer.mozilla.org/en-US/docs/Web/HTTP/Status[/url]. |
| 17 | +func status_ok() -> bool: |
| 18 | + return status >= 200 and status < 300 |
| 19 | + |
| 20 | +## Checks whether the [member status] is between 400 and 599 (inclusive), see [url]https://developer.mozilla.org/en-US/docs/Web/HTTP/Status[/url]. |
| 21 | +func status_err() -> bool: |
| 22 | + return status >= 400 and status < 599 |
| 23 | + |
| 24 | +## The response body as a [String].[br] |
| 25 | +## For other formatting (ascii, utf16, ...) or special use-cases (file I/O, ...), it is possible to access the raw body's [member bytes].[br] |
| 26 | +## You should cache this return value instead of calling the funciton multiple times. |
| 27 | +func body_as_string() -> String: |
| 28 | + return bytes.get_string_from_utf8() |
| 29 | + |
| 30 | +## Attempt to parse the response [member bytes] into a [Dictionary] or [Array], returns null on failure.[br][br] |
| 31 | +## It is possible to cast the return type to a [Dictionary] with "[code]as Dictionary[/code]" to receive autocomplete and other benefits when the parsing was successful.[br] |
| 32 | +## If you want error handling for the JSON deserialization, make an instance of [JSON] and call [method JSON.parse] on it, passing in the return value of [method HTTPResult.body_as_string]. This allows the usage of [method JSON.get_error_message] and [method JSON.get_error_line] to get potential error information.[br][br] |
| 33 | +## [b]Note:[/b] Godot always converts JSON numbers to [float]s! |
| 34 | +func body_as_json() -> Variant: |
| 35 | + return JSON.parse_string(body_as_string()) |
| 36 | + |
| 37 | +# Constructs a new [HTTPResult] from an [enum @GlobalScope.Error] code. (Used internally, hidden from API list) |
| 38 | +static func _from_error(err: Error) -> HTTPResult: |
| 39 | + var h := HTTPResult.new() |
| 40 | + h._error = err |
| 41 | + return h |
| 42 | + |
| 43 | +# Constructs a new [HTTPResult] from the return value of [signal HTTPRequest.request_completed]. (Used internally, hidden from API list) |
| 44 | +@warning_ignore("unsafe_cast") |
| 45 | +static func _from_array(a: Array) -> HTTPResult: |
| 46 | + var h := HTTPResult.new() |
| 47 | + h._result = a[0] as HTTPRequest.Result |
| 48 | + h.status = a[1] as int |
| 49 | + h.headers = _headers_to_dict(a[2] as PackedStringArray) |
| 50 | + h.bytes = a[3] as PackedByteArray |
| 51 | + return h |
| 52 | + |
| 53 | +# Converts a [PackedStringArray] of headers into a [Dictionary]. The header names will be in lowercase, as some web servers prefer this approach and them being case-insensitive as per specification. Therefore, it is good practice to not rely on capitalization. (Used internally, hidden from API list) |
| 54 | +static func _headers_to_dict(headers_arr: PackedStringArray) -> Dictionary: |
| 55 | + var dict := {} |
| 56 | + for h in headers_arr: |
| 57 | + var split := h.split(":") |
| 58 | + dict[split[0].to_lower()] = split[1].strip_edges() |
| 59 | + |
| 60 | + return dict |
0 commit comments