class_httprequest.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the HTTPRequest.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_HTTPRequest:
  6. HTTPRequest
  7. ===========
  8. **Inherits:** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
  9. **Category:** Core
  10. Brief Description
  11. -----------------
  12. A node with the ability to send HTTP(S) requests.
  13. Properties
  14. ----------
  15. +-----------------------------+----------------------------------------------------------------------------+-------+
  16. | :ref:`int<class_int>` | :ref:`body_size_limit<class_HTTPRequest_property_body_size_limit>` | -1 |
  17. +-----------------------------+----------------------------------------------------------------------------+-------+
  18. | :ref:`int<class_int>` | :ref:`download_chunk_size<class_HTTPRequest_property_download_chunk_size>` | 4096 |
  19. +-----------------------------+----------------------------------------------------------------------------+-------+
  20. | :ref:`String<class_String>` | :ref:`download_file<class_HTTPRequest_property_download_file>` | "" |
  21. +-----------------------------+----------------------------------------------------------------------------+-------+
  22. | :ref:`int<class_int>` | :ref:`max_redirects<class_HTTPRequest_property_max_redirects>` | 8 |
  23. +-----------------------------+----------------------------------------------------------------------------+-------+
  24. | :ref:`int<class_int>` | :ref:`timeout<class_HTTPRequest_property_timeout>` | 0 |
  25. +-----------------------------+----------------------------------------------------------------------------+-------+
  26. | :ref:`bool<class_bool>` | :ref:`use_threads<class_HTTPRequest_property_use_threads>` | false |
  27. +-----------------------------+----------------------------------------------------------------------------+-------+
  28. Methods
  29. -------
  30. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | void | :ref:`cancel_request<class_HTTPRequest_method_cancel_request>` **(** **)** |
  32. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`int<class_int>` | :ref:`get_body_size<class_HTTPRequest_method_get_body_size>` **(** **)** const |
  34. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`int<class_int>` | :ref:`get_downloaded_bytes<class_HTTPRequest_method_get_downloaded_bytes>` **(** **)** const |
  36. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`Status<enum_HTTPClient_Status>` | :ref:`get_http_client_status<class_HTTPRequest_method_get_http_client_status>` **(** **)** const |
  38. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`request<class_HTTPRequest_method_request>` **(** :ref:`String<class_String>` url, :ref:`PoolStringArray<class_PoolStringArray>` custom_headers=PoolStringArray( ), :ref:`bool<class_bool>` ssl_validate_domain=true, :ref:`Method<enum_HTTPClient_Method>` method=0, :ref:`String<class_String>` request_data="" **)** |
  40. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. Signals
  42. -------
  43. .. _class_HTTPRequest_signal_request_completed:
  44. - **request_completed** **(** :ref:`int<class_int>` result, :ref:`int<class_int>` response_code, :ref:`PoolStringArray<class_PoolStringArray>` headers, :ref:`PoolByteArray<class_PoolByteArray>` body **)**
  45. Emitted when a request is completed.
  46. Enumerations
  47. ------------
  48. .. _enum_HTTPRequest_Result:
  49. .. _class_HTTPRequest_constant_RESULT_SUCCESS:
  50. .. _class_HTTPRequest_constant_RESULT_CHUNKED_BODY_SIZE_MISMATCH:
  51. .. _class_HTTPRequest_constant_RESULT_CANT_CONNECT:
  52. .. _class_HTTPRequest_constant_RESULT_CANT_RESOLVE:
  53. .. _class_HTTPRequest_constant_RESULT_CONNECTION_ERROR:
  54. .. _class_HTTPRequest_constant_RESULT_SSL_HANDSHAKE_ERROR:
  55. .. _class_HTTPRequest_constant_RESULT_NO_RESPONSE:
  56. .. _class_HTTPRequest_constant_RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  57. .. _class_HTTPRequest_constant_RESULT_REQUEST_FAILED:
  58. .. _class_HTTPRequest_constant_RESULT_DOWNLOAD_FILE_CANT_OPEN:
  59. .. _class_HTTPRequest_constant_RESULT_DOWNLOAD_FILE_WRITE_ERROR:
  60. .. _class_HTTPRequest_constant_RESULT_REDIRECT_LIMIT_REACHED:
  61. .. _class_HTTPRequest_constant_RESULT_TIMEOUT:
  62. enum **Result**:
  63. - **RESULT_SUCCESS** = **0** --- Request successful.
  64. - **RESULT_CHUNKED_BODY_SIZE_MISMATCH** = **1**
  65. - **RESULT_CANT_CONNECT** = **2** --- Request failed while connecting.
  66. - **RESULT_CANT_RESOLVE** = **3** --- Request failed while resolving.
  67. - **RESULT_CONNECTION_ERROR** = **4** --- Request failed due to connection (read/write) error.
  68. - **RESULT_SSL_HANDSHAKE_ERROR** = **5** --- Request failed on SSL handshake.
  69. - **RESULT_NO_RESPONSE** = **6** --- Request does not have a response (yet).
  70. - **RESULT_BODY_SIZE_LIMIT_EXCEEDED** = **7** --- Request exceeded its maximum size limit, see :ref:`body_size_limit<class_HTTPRequest_property_body_size_limit>`.
  71. - **RESULT_REQUEST_FAILED** = **8** --- Request failed. (Unused)
  72. - **RESULT_DOWNLOAD_FILE_CANT_OPEN** = **9** --- HTTPRequest couldn't open the download file.
  73. - **RESULT_DOWNLOAD_FILE_WRITE_ERROR** = **10** --- HTTPRequest couldn't write to the download file.
  74. - **RESULT_REDIRECT_LIMIT_REACHED** = **11** --- Request reached its maximum redirect limit, see :ref:`max_redirects<class_HTTPRequest_property_max_redirects>`.
  75. - **RESULT_TIMEOUT** = **12**
  76. Description
  77. -----------
  78. A node with the ability to send HTTP requests. Uses :ref:`HTTPClient<class_HTTPClient>` internally.
  79. Can be used to make HTTP requests, i.e. download or upload files or web content via HTTP.
  80. **Example of loading and displaying an image using HTTPRequest:**
  81. ::
  82. func _ready():
  83. # Create an HTTP request node and connect its completion signal.
  84. var http_request = HTTPRequest.new()
  85. add_child(http_request)
  86. http_request.connect("request_completed", self, "_http_request_completed")
  87. # Perform the HTTP request. The URL below returns a PNG image as of writing.
  88. var error = http_request.request("https://via.placeholder.com/512")
  89. if error != OK:
  90. push_error("An error occurred in the HTTP request.")
  91. # Called when the HTTP request is completed.
  92. func _http_request_completed(result, response_code, headers, body):
  93. var image = Image.new()
  94. var error = image.load_png_from_buffer(body)
  95. if error != OK:
  96. push_error("Couldn't load the image.")
  97. var texture = ImageTexture.new()
  98. texture.create_from_image(image)
  99. # Display the image in a TextureRect node.
  100. var texture_rect = TextureRect.new()
  101. add_child(texture_rect)
  102. texture_rect.texture = texture
  103. Tutorials
  104. ---------
  105. - :doc:`../tutorials/networking/http_request_class`
  106. - :doc:`../tutorials/networking/ssl_certificates`
  107. Property Descriptions
  108. ---------------------
  109. .. _class_HTTPRequest_property_body_size_limit:
  110. - :ref:`int<class_int>` **body_size_limit**
  111. +-----------+----------------------------+
  112. | *Default* | -1 |
  113. +-----------+----------------------------+
  114. | *Setter* | set_body_size_limit(value) |
  115. +-----------+----------------------------+
  116. | *Getter* | get_body_size_limit() |
  117. +-----------+----------------------------+
  118. Maximum allowed size for response bodies.
  119. ----
  120. .. _class_HTTPRequest_property_download_chunk_size:
  121. - :ref:`int<class_int>` **download_chunk_size**
  122. +-----------+--------------------------------+
  123. | *Default* | 4096 |
  124. +-----------+--------------------------------+
  125. | *Setter* | set_download_chunk_size(value) |
  126. +-----------+--------------------------------+
  127. | *Getter* | get_download_chunk_size() |
  128. +-----------+--------------------------------+
  129. The size of the buffer used and maximum bytes to read per iteration. See :ref:`HTTPClient.read_chunk_size<class_HTTPClient_property_read_chunk_size>`.
  130. Set this to a higher value (e.g. 65536 for 64 KiB) when downloading large files to achieve better speeds at the cost of memory.
  131. ----
  132. .. _class_HTTPRequest_property_download_file:
  133. - :ref:`String<class_String>` **download_file**
  134. +-----------+--------------------------+
  135. | *Default* | "" |
  136. +-----------+--------------------------+
  137. | *Setter* | set_download_file(value) |
  138. +-----------+--------------------------+
  139. | *Getter* | get_download_file() |
  140. +-----------+--------------------------+
  141. The file to download into. Will output any received file into it.
  142. ----
  143. .. _class_HTTPRequest_property_max_redirects:
  144. - :ref:`int<class_int>` **max_redirects**
  145. +-----------+--------------------------+
  146. | *Default* | 8 |
  147. +-----------+--------------------------+
  148. | *Setter* | set_max_redirects(value) |
  149. +-----------+--------------------------+
  150. | *Getter* | get_max_redirects() |
  151. +-----------+--------------------------+
  152. Maximum number of allowed redirects.
  153. ----
  154. .. _class_HTTPRequest_property_timeout:
  155. - :ref:`int<class_int>` **timeout**
  156. +-----------+--------------------+
  157. | *Default* | 0 |
  158. +-----------+--------------------+
  159. | *Setter* | set_timeout(value) |
  160. +-----------+--------------------+
  161. | *Getter* | get_timeout() |
  162. +-----------+--------------------+
  163. ----
  164. .. _class_HTTPRequest_property_use_threads:
  165. - :ref:`bool<class_bool>` **use_threads**
  166. +-----------+------------------------+
  167. | *Default* | false |
  168. +-----------+------------------------+
  169. | *Setter* | set_use_threads(value) |
  170. +-----------+------------------------+
  171. | *Getter* | is_using_threads() |
  172. +-----------+------------------------+
  173. If ``true``, multithreading is used to improve performance.
  174. Method Descriptions
  175. -------------------
  176. .. _class_HTTPRequest_method_cancel_request:
  177. - void **cancel_request** **(** **)**
  178. Cancels the current request.
  179. ----
  180. .. _class_HTTPRequest_method_get_body_size:
  181. - :ref:`int<class_int>` **get_body_size** **(** **)** const
  182. Returns the response body length.
  183. **Note:** Some Web servers may not send a body length. In this case, the value returned will be ``-1``. If using chunked transfer encoding, the body length will also be ``-1``.
  184. ----
  185. .. _class_HTTPRequest_method_get_downloaded_bytes:
  186. - :ref:`int<class_int>` **get_downloaded_bytes** **(** **)** const
  187. Returns the amount of bytes this HTTPRequest downloaded.
  188. ----
  189. .. _class_HTTPRequest_method_get_http_client_status:
  190. - :ref:`Status<enum_HTTPClient_Status>` **get_http_client_status** **(** **)** const
  191. Returns the current status of the underlying :ref:`HTTPClient<class_HTTPClient>`. See :ref:`Status<enum_HTTPClient_Status>`.
  192. ----
  193. .. _class_HTTPRequest_method_request:
  194. - :ref:`Error<enum_@GlobalScope_Error>` **request** **(** :ref:`String<class_String>` url, :ref:`PoolStringArray<class_PoolStringArray>` custom_headers=PoolStringArray( ), :ref:`bool<class_bool>` ssl_validate_domain=true, :ref:`Method<enum_HTTPClient_Method>` method=0, :ref:`String<class_String>` request_data="" **)**
  195. Creates request on the underlying :ref:`HTTPClient<class_HTTPClient>`. If there is no configuration errors, it tries to connect using :ref:`HTTPClient.connect_to_host<class_HTTPClient_method_connect_to_host>` and passes parameters onto :ref:`HTTPClient.request<class_HTTPClient_method_request>`.
  196. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` if request is successfully created. (Does not imply that the server has responded), :ref:`@GlobalScope.ERR_UNCONFIGURED<class_@GlobalScope_constant_ERR_UNCONFIGURED>` if not in the tree, :ref:`@GlobalScope.ERR_BUSY<class_@GlobalScope_constant_ERR_BUSY>` if still processing previous request, :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>` if given string is not a valid URL format, or :ref:`@GlobalScope.ERR_CANT_CONNECT<class_@GlobalScope_constant_ERR_CANT_CONNECT>` if not using thread and the :ref:`HTTPClient<class_HTTPClient>` cannot connect to host.