http_client_class.rst 3.1 KB

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