diff options
Diffstat (limited to 'doc/classes/HTTPClient.xml')
-rw-r--r-- | doc/classes/HTTPClient.xml | 159 |
1 files changed, 90 insertions, 69 deletions
diff --git a/doc/classes/HTTPClient.xml b/doc/classes/HTTPClient.xml index 76153ae041..e15b065744 100644 --- a/doc/classes/HTTPClient.xml +++ b/doc/classes/HTTPClient.xml @@ -1,37 +1,36 @@ <?xml version="1.0" encoding="UTF-8" ?> -<class name="HTTPClient" inherits="Reference" version="4.0"> +<class name="HTTPClient" inherits="RefCounted" version="4.0"> <brief_description> - Hyper-text transfer protocol client. + Low-level hyper-text transfer protocol client. </brief_description> <description> - Hyper-text transfer protocol client (sometimes called "User Agent"). Used to make HTTP requests to download web content, upload files and other data or to communicate with various services, among other use cases. See [HTTPRequest] for an higher-level alternative. + Hyper-text transfer protocol client (sometimes called "User Agent"). Used to make HTTP requests to download web content, upload files and other data or to communicate with various services, among other use cases. [b]See the [HTTPRequest] node for a higher-level alternative.[/b] [b]Note:[/b] This client only needs to connect to a host once (see [method connect_to_host]) to send multiple requests. Because of this, methods that take URLs usually take just the part after the host instead of the full URL, as the client is already connected to a host. See [method request] for a full example and to get started. A [HTTPClient] should be reused between multiple requests or to connect to different hosts instead of creating one client per request. Supports SSL and SSL server certificate verification. HTTP status codes in the 2xx range indicate success, 3xx redirection (i.e. "try again, but over here"), 4xx something was wrong with the request, and 5xx something went wrong on the server's side. For more information on HTTP, see https://developer.mozilla.org/en-US/docs/Web/HTTP (or read RFC 2616 to get it straight from the source: https://tools.ietf.org/html/rfc2616). + [b]Note:[/b] When exporting to Android, make sure to enable the [code]INTERNET[/code] permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android. + [b]Note:[/b] It's recommended to use transport encryption (SSL/TLS) and to avoid sending sensitive information (such as login credentials) in HTTP GET URL parameters. Consider using HTTP POST requests or HTTP headers for such information instead. + [b]Note:[/b] When performing HTTP requests from a project exported to HTML5, keep in mind the remote server may not allow requests from foreign origins due to [url=https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS]CORS[/url]. If you host the server in question, you should modify its backend to allow requests from foreign origins by adding the [code]Access-Control-Allow-Origin: *[/code] HTTP header. + [b]Note:[/b] SSL/TLS support is currently limited to TLS 1.0, TLS 1.1, and TLS 1.2. Attempting to connect to a TLS 1.3-only server will return an error. + [b]Warning:[/b] SSL/TLS certificate revocation and certificate pinning are currently not supported. Revoked certificates are accepted as long as they are otherwise valid. If this is a concern, you may want to use automatically managed certificates with a short validity period. </description> <tutorials> - <link>https://docs.godotengine.org/en/latest/tutorials/networking/http_client_class.html</link> - <link>https://docs.godotengine.org/en/latest/tutorials/networking/ssl_certificates.html</link> + <link title="HTTP client class">$DOCS_URL/tutorials/networking/http_client_class.html</link> + <link title="SSL certificates">$DOCS_URL/tutorials/networking/ssl_certificates.html</link> </tutorials> <methods> <method name="close"> - <return type="void"> - </return> + <return type="void" /> <description> Closes the current connection, allowing reuse of this [HTTPClient]. </description> </method> <method name="connect_to_host"> - <return type="int" enum="Error"> - </return> - <argument index="0" name="host" type="String"> - </argument> - <argument index="1" name="port" type="int" default="-1"> - </argument> - <argument index="2" name="use_ssl" type="bool" default="false"> - </argument> - <argument index="3" name="verify_host" type="bool" default="true"> - </argument> + <return type="int" enum="Error" /> + <argument index="0" name="host" type="String" /> + <argument index="1" name="port" type="int" default="-1" /> + <argument index="2" name="use_ssl" type="bool" default="false" /> + <argument index="3" name="verify_host" type="bool" default="true" /> <description> Connects to a host. This needs to be done before any requests are sent. The host should not have http:// prepended but will strip the protocol identifier if provided. @@ -40,30 +39,26 @@ </description> </method> <method name="get_response_body_length" qualifiers="const"> - <return type="int"> - </return> + <return type="int" /> <description> Returns the response's body length. [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_response_code" qualifiers="const"> - <return type="int"> - </return> + <return type="int" /> <description> Returns the response's HTTP status code. </description> </method> <method name="get_response_headers"> - <return type="PackedStringArray"> - </return> + <return type="PackedStringArray" /> <description> Returns the response headers. </description> </method> <method name="get_response_headers_as_dictionary"> - <return type="Dictionary"> - </return> + <return type="Dictionary" /> <description> Returns all response headers as a Dictionary of structure [code]{ "key": "value1; value2" }[/code] where the case-sensitivity of the keys and values is kept like the server delivers it. A value is a simple String, this string can have more than one value where "; " is used as separator. [b]Example:[/b] @@ -76,100 +71,126 @@ </description> </method> <method name="get_status" qualifiers="const"> - <return type="int" enum="HTTPClient.Status"> - </return> + <return type="int" enum="HTTPClient.Status" /> <description> Returns a [enum Status] constant. Need to call [method poll] in order to get status updates. </description> </method> <method name="has_response" qualifiers="const"> - <return type="bool"> - </return> + <return type="bool" /> <description> If [code]true[/code], this [HTTPClient] has a response available. </description> </method> <method name="is_response_chunked" qualifiers="const"> - <return type="bool"> - </return> + <return type="bool" /> <description> If [code]true[/code], this [HTTPClient] has a response that is chunked. </description> </method> <method name="poll"> - <return type="int" enum="Error"> - </return> + <return type="int" enum="Error" /> <description> This needs to be called in order to have any request processed. Check results with [method get_status]. </description> </method> <method name="query_string_from_dict"> - <return type="String"> - </return> - <argument index="0" name="fields" type="Dictionary"> - </argument> + <return type="String" /> + <argument index="0" name="fields" type="Dictionary" /> <description> Generates a GET/POST application/x-www-form-urlencoded style query string from a provided dictionary, e.g.: - [codeblock] + [codeblocks] + [gdscript] var fields = {"username": "user", "password": "pass"} var query_string = http_client.query_string_from_dict(fields) # Returns "username=user&password=pass" - [/codeblock] + [/gdscript] + [csharp] + var fields = new Godot.Collections.Dictionary { { "username", "user" }, { "password", "pass" } }; + string queryString = new HTTPClient().QueryStringFromDict(fields); + // Returns "username=user&password=pass" + [/csharp] + [/codeblocks] Furthermore, if a key has a [code]null[/code] value, only the key itself is added, without equal sign and value. If the value is an array, for each value in it a pair with the same key is added. - [codeblock] + [codeblocks] + [gdscript] var fields = {"single": 123, "not_valued": null, "multiple": [22, 33, 44]} var query_string = http_client.query_string_from_dict(fields) # Returns "single=123&not_valued&multiple=22&multiple=33&multiple=44" - [/codeblock] + [/gdscript] + [csharp] + var fields = new Godot.Collections.Dictionary{{"single", 123}, {"notValued", null}, {"multiple", new Godot.Collections.Array{22, 33, 44}}}; + string queryString = new HTTPClient().QueryStringFromDict(fields); + // Returns "single=123&not_valued&multiple=22&multiple=33&multiple=44" + [/csharp] + [/codeblocks] </description> </method> <method name="read_response_body_chunk"> - <return type="PackedByteArray"> - </return> + <return type="PackedByteArray" /> <description> Reads one chunk from the response. </description> </method> <method name="request"> - <return type="int" enum="Error"> - </return> - <argument index="0" name="method" type="int" enum="HTTPClient.Method"> - </argument> - <argument index="1" name="url" type="String"> - </argument> - <argument index="2" name="headers" type="PackedStringArray"> - </argument> - <argument index="3" name="body" type="String" default=""""> - </argument> + <return type="int" enum="Error" /> + <argument index="0" name="method" type="int" enum="HTTPClient.Method" /> + <argument index="1" name="url" type="String" /> + <argument index="2" name="headers" type="PackedStringArray" /> + <argument index="3" name="body" type="String" default="""" /> <description> - Sends a request to the connected host. The URL parameter is just the part after the host, so for [code]http://somehost.com/index.php[/code], it is [code]index.php[/code]. + Sends a request to the connected host. + The URL parameter is usually just the part after the host, so for [code]https://somehost.com/index.php[/code], it is [code]/index.php[/code]. When sending requests to an HTTP proxy server, it should be an absolute URL. For [constant HTTPClient.METHOD_OPTIONS] requests, [code]*[/code] is also allowed. For [constant HTTPClient.METHOD_CONNECT] requests, it should be the authority component ([code]host:port[/code]). Headers are HTTP request headers. For available HTTP methods, see [enum Method]. To create a POST request with query strings to push to the server, do: - [codeblock] + [codeblocks] + [gdscript] var fields = {"username" : "user", "password" : "pass"} var query_string = http_client.query_string_from_dict(fields) var headers = ["Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(query_string.length())] - var result = http_client.request(http_client.METHOD_POST, "index.php", headers, query_string) - [/codeblock] + var result = http_client.request(http_client.METHOD_POST, "/index.php", headers, query_string) + [/gdscript] + [csharp] + var fields = new Godot.Collections.Dictionary { { "username", "user" }, { "password", "pass" } }; + string queryString = new HTTPClient().QueryStringFromDict(fields); + string[] headers = {"Content-Type: application/x-www-form-urlencoded", "Content-Length: " + queryString.Length}; + var result = new HTTPClient().Request(HTTPClient.Method.Post, "index.php", headers, queryString); + [/csharp] + [/codeblocks] + [b]Note:[/b] The [code]request_data[/code] parameter is ignored if [code]method[/code] is [constant HTTPClient.METHOD_GET]. This is because GET methods can't contain request data. As a workaround, you can pass request data as a query string in the URL. See [method String.uri_encode] for an example. </description> </method> <method name="request_raw"> - <return type="int" enum="Error"> - </return> - <argument index="0" name="method" type="int" enum="HTTPClient.Method"> - </argument> - <argument index="1" name="url" type="String"> - </argument> - <argument index="2" name="headers" type="PackedStringArray"> - </argument> - <argument index="3" name="body" type="PackedByteArray"> - </argument> + <return type="int" enum="Error" /> + <argument index="0" name="method" type="int" enum="HTTPClient.Method" /> + <argument index="1" name="url" type="String" /> + <argument index="2" name="headers" type="PackedStringArray" /> + <argument index="3" name="body" type="PackedByteArray" /> <description> - Sends a raw request to the connected host. The URL parameter is just the part after the host, so for [code]http://somehost.com/index.php[/code], it is [code]index.php[/code]. + Sends a raw request to the connected host. + The URL parameter is usually just the part after the host, so for [code]https://somehost.com/index.php[/code], it is [code]/index.php[/code]. When sending requests to an HTTP proxy server, it should be an absolute URL. For [constant HTTPClient.METHOD_OPTIONS] requests, [code]*[/code] is also allowed. For [constant HTTPClient.METHOD_CONNECT] requests, it should be the authority component ([code]host:port[/code]). Headers are HTTP request headers. For available HTTP methods, see [enum Method]. Sends the body data raw, as a byte array and does not encode it in any way. </description> </method> + <method name="set_http_proxy"> + <return type="void" /> + <argument index="0" name="host" type="String" /> + <argument index="1" name="port" type="int" /> + <description> + Sets the proxy server for HTTP requests. + The proxy server is unset if [code]host[/code] is empty or [code]port[/code] is -1. + </description> + </method> + <method name="set_https_proxy"> + <return type="void" /> + <argument index="0" name="host" type="String" /> + <argument index="1" name="port" type="int" /> + <description> + Sets the proxy server for HTTPS requests. + The proxy server is unset if [code]host[/code] is empty or [code]port[/code] is -1. + </description> + </method> </methods> <members> <member name="blocking_mode_enabled" type="bool" setter="set_blocking_mode" getter="is_blocking_mode_enabled" default="false"> @@ -178,7 +199,7 @@ <member name="connection" type="StreamPeer" setter="set_connection" getter="get_connection"> The connection to use for this client. </member> - <member name="read_chunk_size" type="int" setter="set_read_chunk_size" getter="get_read_chunk_size" default="4096"> + <member name="read_chunk_size" type="int" setter="set_read_chunk_size" getter="get_read_chunk_size" default="65536"> The size of the buffer used and maximum bytes to read per iteration. See [method read_response_body_chunk]. </member> </members> |