mirror of
https://github.com/godotengine/godot.git
synced 2024-09-20 06:22:24 +00:00
HTTPRequest
making working with headers easier
Closes https://github.com/godotengine/godot-proposals/issues/4734 Adding `HTTPRequest::get_dictionary_from_headers` to get a Dictionary from `headers` parameter on `request_completed`. Making `HTTPRequest::has_header` and `HTTPRequest::get_header_value` public. Co-Authored-By: =?UTF-8?q?Jakub=20Jan=C5=A1ta?= <jansta.ja@gmail.com>
This commit is contained in:
parent
694d3c2930
commit
70338e142e
|
@ -117,7 +117,7 @@ Dictionary HTTPClient::_get_response_headers_as_dictionary() {
|
|||
if (sp == -1) {
|
||||
continue;
|
||||
}
|
||||
String key = s.substr(0, sp).strip_edges();
|
||||
String key = s.substr(0, sp).strip_edges().to_lower();
|
||||
String value = s.substr(sp + 1, s.length()).strip_edges();
|
||||
ret[key] = value;
|
||||
}
|
||||
|
|
|
@ -59,11 +59,11 @@
|
|||
<method name="get_response_headers_as_dictionary">
|
||||
<return type="Dictionary" />
|
||||
<description>
|
||||
Returns all response headers as a [Dictionary]. Each entry is composed by the header name, and a [String] containing the values separated by [code]"; "[/code]. The casing is kept the same as the headers were received.
|
||||
Returns all response headers as a [Dictionary] of structure [code]{ "key": "value1; value2" }[/code] where the case-sensitivity is ignored and converted to lower-case keys. A value is a simple [String], this string can have more than one value where "; " is used as separator.
|
||||
[codeblock]
|
||||
{
|
||||
"content-length": 12,
|
||||
"Content-Type": "application/json; charset=UTF-8",
|
||||
"content-type": "application/json; charset=UTF-8",
|
||||
}
|
||||
[/codeblock]
|
||||
</description>
|
||||
|
|
|
@ -170,18 +170,53 @@
|
|||
[b]Note:[/b] Some Web servers may not send a body length. In this case, the value returned will be [code]-1[/code]. If using chunked transfer encoding, the body length will also be [code]-1[/code].
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_dictionary_from_headers">
|
||||
<return type="Dictionary" />
|
||||
<param index="0" name="headers" type="PackedStringArray" />
|
||||
<description>
|
||||
Returns all response headers as a [Dictionary] of structure [code]{ "key": "value1; value2" }[/code] where the case-sensitivity is ignored and converted to lower-case keys. A value is a simple [String], this string can have more than one value where "; " is used as separator.
|
||||
[b]Example:[/b]
|
||||
[codeblock]
|
||||
{
|
||||
"content-length": 12,
|
||||
"content-type": "application/json; charset=UTF-8",
|
||||
}
|
||||
[/codeblock]
|
||||
[b]Note:[/b] [param headers] are meant to be supplied with [param headers] returned via [signal request_completed].
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_downloaded_bytes" qualifiers="const">
|
||||
<return type="int" />
|
||||
<description>
|
||||
Returns the number of bytes this HTTPRequest downloaded.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_header_value">
|
||||
<return type="String" />
|
||||
<param index="0" name="headers" type="PackedStringArray" />
|
||||
<param index="1" name="header_name" type="String" />
|
||||
<description>
|
||||
Returns the value of the header with the [param header_name] as [String] if found in [param headers]. Otherwise returns an empty [String].
|
||||
[b]Note:[/b] [param headers] are meant to be supplied with [param headers] returned via [signal request_completed].
|
||||
[b]Note:[/b] Header names are case-insensitive.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_http_client_status" qualifiers="const">
|
||||
<return type="int" enum="HTTPClient.Status" />
|
||||
<description>
|
||||
Returns the current status of the underlying [HTTPClient]. See [enum HTTPClient.Status].
|
||||
</description>
|
||||
</method>
|
||||
<method name="has_header">
|
||||
<return type="bool" />
|
||||
<param index="0" name="headers" type="PackedStringArray" />
|
||||
<param index="1" name="header_name" type="String" />
|
||||
<description>
|
||||
Returns [code]true[/code] if given [param header_name] is found in [param headers]. Otherwise returns [code]false[/code].
|
||||
[b]Note:[/b] [param headers] are meant to be supplied with [param headers] returned via [signal request_completed].
|
||||
[b]Note:[/b] Header names are case-insensitive.
|
||||
</description>
|
||||
</method>
|
||||
<method name="request">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="url" type="String" />
|
||||
|
|
|
@ -84,11 +84,11 @@ bool HTTPRequest::has_header(const PackedStringArray &p_headers, const String &p
|
|||
String HTTPRequest::get_header_value(const PackedStringArray &p_headers, const String &p_header_name) {
|
||||
String value = "";
|
||||
|
||||
String lowwer_case_header_name = p_header_name.to_lower();
|
||||
String lower_case_header_name = p_header_name.to_lower();
|
||||
for (int i = 0; i < p_headers.size(); i++) {
|
||||
if (p_headers[i].find(":") > 0) {
|
||||
Vector<String> parts = p_headers[i].split(":", false, 1);
|
||||
if (parts.size() > 1 && parts[0].strip_edges().to_lower() == lowwer_case_header_name) {
|
||||
if (parts.size() > 1 && parts[0].strip_edges().to_lower() == lower_case_header_name) {
|
||||
value = parts[1].strip_edges();
|
||||
break;
|
||||
}
|
||||
|
@ -599,6 +599,21 @@ void HTTPRequest::set_tls_options(const Ref<TLSOptions> &p_options) {
|
|||
tls_options = p_options;
|
||||
}
|
||||
|
||||
Dictionary HTTPRequest::get_dictionary_from_headers(const PackedStringArray &p_headers) {
|
||||
Dictionary ret;
|
||||
for (const String &s : p_headers) {
|
||||
int sp = s.find(":");
|
||||
if (sp == -1) {
|
||||
continue;
|
||||
}
|
||||
String key = s.substr(0, sp).strip_edges().to_lower();
|
||||
String value = s.substr(sp + 1, s.length()).strip_edges();
|
||||
ret[key] = value;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void HTTPRequest::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("request", "url", "custom_headers", "method", "request_data"), &HTTPRequest::request, DEFVAL(PackedStringArray()), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(String()));
|
||||
ClassDB::bind_method(D_METHOD("request_raw", "url", "custom_headers", "method", "request_data_raw"), &HTTPRequest::request_raw, DEFVAL(PackedStringArray()), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(PackedByteArray()));
|
||||
|
@ -634,6 +649,11 @@ void HTTPRequest::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("set_http_proxy", "host", "port"), &HTTPRequest::set_http_proxy);
|
||||
ClassDB::bind_method(D_METHOD("set_https_proxy", "host", "port"), &HTTPRequest::set_https_proxy);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("has_header", "headers", "header_name"), &HTTPRequest::has_header);
|
||||
ClassDB::bind_method(D_METHOD("get_header_value", "headers", "header_name"), &HTTPRequest::get_header_value);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_dictionary_from_headers", "headers"), &HTTPRequest::get_dictionary_from_headers);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "download_file", PROPERTY_HINT_FILE), "set_download_file", "get_download_file");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "download_chunk_size", PROPERTY_HINT_RANGE, "256,16777216,suffix:B"), "set_download_chunk_size", "get_download_chunk_size");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "is_using_threads");
|
||||
|
|
|
@ -108,9 +108,6 @@ private:
|
|||
Error _parse_url(const String &p_url);
|
||||
Error _request();
|
||||
|
||||
bool has_header(const PackedStringArray &p_headers, const String &p_header_name);
|
||||
String get_header_value(const PackedStringArray &p_headers, const String &header_name);
|
||||
|
||||
SafeFlag thread_done;
|
||||
SafeFlag thread_request_quit;
|
||||
|
||||
|
@ -163,6 +160,11 @@ public:
|
|||
|
||||
void set_tls_options(const Ref<TLSOptions> &p_options);
|
||||
|
||||
bool has_header(const PackedStringArray &p_headers, const String &p_header_name);
|
||||
String get_header_value(const PackedStringArray &p_headers, const String &p_header_name);
|
||||
|
||||
Dictionary get_dictionary_from_headers(const PackedStringArray &p_headers);
|
||||
|
||||
HTTPRequest();
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user