http_client_class.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. .. _doc_http_client_class:
  2. HTTP client class
  3. =================
  4. :ref:`HTTPClient <class_HTTPClient>` provides low-level access to HTTP communication.
  5. For a higher-level interface, you may want to take a look at :ref:`HTTPRequest <class_HTTPRequest>` first,
  6. which has a tutorial available :ref:`here <doc_http_request_class>`.
  7. Here's an example of using the :ref:`HTTPClient <class_HTTPClient>`
  8. class. It's just a script, so it can be run by executing:
  9. ::
  10. c:\godot> godot -s http_test.gd
  11. It will connect and fetch a website.
  12. ::
  13. extends SceneTree
  14. # HTTPClient demo
  15. # This simple class can do HTTP requests; it will not block, but it needs to be polled.
  16. func _init():
  17. var err = 0
  18. var http = HTTPClient.new() # Create the Client.
  19. err = http.connect_to_host("www.php.net", 80) # Connect to host/port.
  20. assert(err == OK) # Make sure connection was OK.
  21. # Wait until resolved and connected.
  22. while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
  23. http.poll()
  24. print("Connecting...")
  25. OS.delay_msec(500)
  26. assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
  27. # Some headers
  28. var headers = [
  29. "User-Agent: Pirulo/1.0 (Godot)",
  30. "Accept: */*"
  31. ]
  32. err = http.request(HTTPClient.METHOD_GET, "/ChangeLog-5.php", headers) # Request a page from the site (this one was chunked..)
  33. assert(err == OK) # Make sure all is OK.
  34. while http.get_status() == HTTPClient.STATUS_REQUESTING:
  35. # Keep polling for as long as the request is being processed.
  36. http.poll()
  37. print("Requesting...")
  38. if not OS.has_feature("web"):
  39. OS.delay_msec(500)
  40. else:
  41. # Synchronous HTTP requests are not supported on the web,
  42. # so wait for the next main loop iteration.
  43. yield(Engine.get_main_loop(), "idle_frame")
  44. assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
  45. print("response? ", http.has_response()) # Site might not have a response.
  46. if http.has_response():
  47. # If there is a response...
  48. headers = http.get_response_headers_as_dictionary() # Get response headers.
  49. print("code: ", http.get_response_code()) # Show response code.
  50. print("**headers:\\n", headers) # Show headers.
  51. # Getting the HTTP Body
  52. if http.is_response_chunked():
  53. # Does it use chunks?
  54. print("Response is Chunked!")
  55. else:
  56. # Or just plain Content-Length
  57. var bl = http.get_response_body_length()
  58. print("Response Length: ",bl)
  59. # This method works for both anyway
  60. var rb = PoolByteArray() # Array that will hold the data.
  61. while http.get_status() == HTTPClient.STATUS_BODY:
  62. # While there is body left to be read
  63. http.poll()
  64. var chunk = http.read_response_body_chunk() # Get a chunk.
  65. if chunk.size() == 0:
  66. # Got nothing, wait for buffers to fill a bit.
  67. OS.delay_usec(1000)
  68. else:
  69. rb = rb + chunk # Append to read buffer.
  70. # Done!
  71. print("bytes got: ", rb.size())
  72. var text = rb.get_string_from_ascii()
  73. print("Text: ", text)
  74. quit()