http_client_class.rst 3.0 KB

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