HTTPRequest.xml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="HTTPRequest" inherits="Node" version="3.2">
  3. <brief_description>
  4. A node with the ability to send HTTP(S) requests.
  5. </brief_description>
  6. <description>
  7. A node with the ability to send HTTP requests. Uses [HTTPClient] internally.
  8. Can be used to make HTTP requests, i.e. download or upload files or web content via HTTP.
  9. [b]Example of contacting a REST API and printing one of its returned fields:[/b]
  10. [codeblock]
  11. func _ready():
  12. # Create an HTTP request node and connect its completion signal.
  13. var http_request = HTTPRequest.new()
  14. add_child(http_request)
  15. http_request.connect("request_completed", self, "_http_request_completed")
  16. # Perform the HTTP request. The URL below returns some JSON as of writing.
  17. var error = http_request.request("https://httpbin.org/get")
  18. if error != OK:
  19. push_error("An error occurred in the HTTP request.")
  20. # Called when the HTTP request is completed.
  21. func _http_request_completed(result, response_code, headers, body):
  22. var response = parse_json(body.get_string_from_utf8())
  23. # Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
  24. print(response.headers["User-Agent"])
  25. [/codeblock]
  26. [b]Example of loading and displaying an image using HTTPRequest:[/b]
  27. [codeblock]
  28. func _ready():
  29. # Create an HTTP request node and connect its completion signal.
  30. var http_request = HTTPRequest.new()
  31. add_child(http_request)
  32. http_request.connect("request_completed", self, "_http_request_completed")
  33. # Perform the HTTP request. The URL below returns a PNG image as of writing.
  34. var error = http_request.request("https://via.placeholder.com/512")
  35. if error != OK:
  36. push_error("An error occurred in the HTTP request.")
  37. # Called when the HTTP request is completed.
  38. func _http_request_completed(result, response_code, headers, body):
  39. var image = Image.new()
  40. var error = image.load_png_from_buffer(body)
  41. if error != OK:
  42. push_error("Couldn't load the image.")
  43. var texture = ImageTexture.new()
  44. texture.create_from_image(image)
  45. # Display the image in a TextureRect node.
  46. var texture_rect = TextureRect.new()
  47. add_child(texture_rect)
  48. texture_rect.texture = texture
  49. [/codeblock]
  50. </description>
  51. <tutorials>
  52. <link>https://docs.godotengine.org/en/latest/tutorials/networking/http_request_class.html</link>
  53. <link>https://docs.godotengine.org/en/latest/tutorials/networking/ssl_certificates.html</link>
  54. </tutorials>
  55. <methods>
  56. <method name="cancel_request">
  57. <return type="void">
  58. </return>
  59. <description>
  60. Cancels the current request.
  61. </description>
  62. </method>
  63. <method name="get_body_size" qualifiers="const">
  64. <return type="int">
  65. </return>
  66. <description>
  67. Returns the response body length.
  68. [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].
  69. </description>
  70. </method>
  71. <method name="get_downloaded_bytes" qualifiers="const">
  72. <return type="int">
  73. </return>
  74. <description>
  75. Returns the amount of bytes this HTTPRequest downloaded.
  76. </description>
  77. </method>
  78. <method name="get_http_client_status" qualifiers="const">
  79. <return type="int" enum="HTTPClient.Status">
  80. </return>
  81. <description>
  82. Returns the current status of the underlying [HTTPClient]. See [enum HTTPClient.Status].
  83. </description>
  84. </method>
  85. <method name="request">
  86. <return type="int" enum="Error">
  87. </return>
  88. <argument index="0" name="url" type="String">
  89. </argument>
  90. <argument index="1" name="custom_headers" type="PoolStringArray" default="PoolStringArray( )">
  91. </argument>
  92. <argument index="2" name="ssl_validate_domain" type="bool" default="true">
  93. </argument>
  94. <argument index="3" name="method" type="int" enum="HTTPClient.Method" default="0">
  95. </argument>
  96. <argument index="4" name="request_data" type="String" default="&quot;&quot;">
  97. </argument>
  98. <description>
  99. Creates request on the underlying [HTTPClient]. If there is no configuration errors, it tries to connect using [method HTTPClient.connect_to_host] and passes parameters onto [method HTTPClient.request].
  100. Returns [constant OK] if request is successfully created. (Does not imply that the server has responded), [constant ERR_UNCONFIGURED] if not in the tree, [constant ERR_BUSY] if still processing previous request, [constant ERR_INVALID_PARAMETER] if given string is not a valid URL format, or [constant ERR_CANT_CONNECT] if not using thread and the [HTTPClient] cannot connect to host.
  101. </description>
  102. </method>
  103. </methods>
  104. <members>
  105. <member name="body_size_limit" type="int" setter="set_body_size_limit" getter="get_body_size_limit" default="-1">
  106. Maximum allowed size for response bodies.
  107. </member>
  108. <member name="download_chunk_size" type="int" setter="set_download_chunk_size" getter="get_download_chunk_size" default="4096">
  109. The size of the buffer used and maximum bytes to read per iteration. See [member HTTPClient.read_chunk_size].
  110. 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.
  111. </member>
  112. <member name="download_file" type="String" setter="set_download_file" getter="get_download_file" default="&quot;&quot;">
  113. The file to download into. Will output any received file into it.
  114. </member>
  115. <member name="max_redirects" type="int" setter="set_max_redirects" getter="get_max_redirects" default="8">
  116. Maximum number of allowed redirects.
  117. </member>
  118. <member name="timeout" type="int" setter="set_timeout" getter="get_timeout" default="0">
  119. </member>
  120. <member name="use_threads" type="bool" setter="set_use_threads" getter="is_using_threads" default="false">
  121. If [code]true[/code], multithreading is used to improve performance.
  122. </member>
  123. </members>
  124. <signals>
  125. <signal name="request_completed">
  126. <argument index="0" name="result" type="int">
  127. </argument>
  128. <argument index="1" name="response_code" type="int">
  129. </argument>
  130. <argument index="2" name="headers" type="PoolStringArray">
  131. </argument>
  132. <argument index="3" name="body" type="PoolByteArray">
  133. </argument>
  134. <description>
  135. Emitted when a request is completed.
  136. </description>
  137. </signal>
  138. </signals>
  139. <constants>
  140. <constant name="RESULT_SUCCESS" value="0" enum="Result">
  141. Request successful.
  142. </constant>
  143. <constant name="RESULT_CHUNKED_BODY_SIZE_MISMATCH" value="1" enum="Result">
  144. </constant>
  145. <constant name="RESULT_CANT_CONNECT" value="2" enum="Result">
  146. Request failed while connecting.
  147. </constant>
  148. <constant name="RESULT_CANT_RESOLVE" value="3" enum="Result">
  149. Request failed while resolving.
  150. </constant>
  151. <constant name="RESULT_CONNECTION_ERROR" value="4" enum="Result">
  152. Request failed due to connection (read/write) error.
  153. </constant>
  154. <constant name="RESULT_SSL_HANDSHAKE_ERROR" value="5" enum="Result">
  155. Request failed on SSL handshake.
  156. </constant>
  157. <constant name="RESULT_NO_RESPONSE" value="6" enum="Result">
  158. Request does not have a response (yet).
  159. </constant>
  160. <constant name="RESULT_BODY_SIZE_LIMIT_EXCEEDED" value="7" enum="Result">
  161. Request exceeded its maximum size limit, see [member body_size_limit].
  162. </constant>
  163. <constant name="RESULT_REQUEST_FAILED" value="8" enum="Result">
  164. Request failed (currently unused).
  165. </constant>
  166. <constant name="RESULT_DOWNLOAD_FILE_CANT_OPEN" value="9" enum="Result">
  167. HTTPRequest couldn't open the download file.
  168. </constant>
  169. <constant name="RESULT_DOWNLOAD_FILE_WRITE_ERROR" value="10" enum="Result">
  170. HTTPRequest couldn't write to the download file.
  171. </constant>
  172. <constant name="RESULT_REDIRECT_LIMIT_REACHED" value="11" enum="Result">
  173. Request reached its maximum redirect limit, see [member max_redirects].
  174. </constant>
  175. <constant name="RESULT_TIMEOUT" value="12" enum="Result">
  176. </constant>
  177. </constants>
  178. </class>