GetAsync
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts.
For thread safety, this property is not safe to read in an unsynchronized thread.
The GetAsync function sends an HTTP GET request. It functions similarly to HttpService/RequestAsync|RequestAsync except that it accepts HTTP request parameters as method parameters instead of a single dictionary and returns only the body of the HTTP response. Generally, this method is useful only as a shorthand and HttpService/RequestAsync|RequestAsync should to be used in most cases. For a detailed guide on sending and retrieving data via HTTP requests, see the Sending HTTP Requests article.
When true, the nocache parameter prevents this function from caching results from previous calls with the same url.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
The web address you are requesting data from |
||
|
false
|
Whether the request stores (caches) the response |
||
|
|
Used to specify some HTTP request headers |
Returns
| Return Type | Summary |
|---|---|
|
The GET request’s response body |
Code Samples
Astronauts in Space
This code sample uses HttpService’s GetAsync to make a request to Open Notify, a web service that provides data from NASA. The request is made to an endpoint that provides information on how many astronauts are currently in space. The response is provided in JSON format, so it is parsed using JSONDecode. Finally, the response data is then processed and printed to the Output.
Test this script by pasting the source code into a Script (HttpService cannot be used by LocalScripts). Also, be sure to enable HTTP Requests in your Game Settings (Home > Game Settings).
Where is the International Space Station?
This code sample uses HttpService’s GetAsync to make a request to an endpoint at Open Notify, a website that provides information from NASA. The endpoint provides information on the current location of the International Space Station. This example uses a defensive coding technique that you should use when making web requests. It wraps the call to GetAsync and JSONDecode in pcall, which protects our script from raising an error if either of these fail. Then, it checks the raw response for all proper data before using it. All of this is put inside a function that returns true or false depending of the request’s success.
Whenever you’re working with web requests, you should prepare for anything to go wrong. Perhaps your web endpoint changes or goes down - you don’t want your game scripts raising errors and breaking your game. You want to handle both success and failure gracefully - have a plan in case your data is not available. Use pcall and make plenty of validity checks (if statements) on your data to make sure you’re getting exactly what you expect.