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.
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
The web address you are requesting data from
Whether the request stores (caches) the response
Used to specify some HTTP request headers
The GET request’s response body
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.
local HttpService = game:GetService("HttpService") -- Where is the International Space Station right now? local URL_ISS = "http://api.open-notify.org/iss-now.json" local function printISS() local response local data -- Use pcall in case something goes wrong pcall(function () response = HttpService:GetAsync(URL_ISS) data = HttpService:JSONDecode(response) end) -- Did our request fail or our JSON fail to parse? if not data then return false end -- Fully check our data for validity. This is dependent on what endpoint you're -- to which you're sending your requests. For this example, this endpoint is -- described here: http://open-notify.org/Open-Notify-API/ISS-Location-Now/ if data.message == "success" and data.iss_position then if data.iss_position.latitude and data.iss_position.longitude then print("The International Space Station is currently at:") print(data.iss_position.latitude .. ", " .. data.iss_position.longitude) return true end end return false end if printISS() then print("Success") else print("Something went wrong") end
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).
local HttpService = game:GetService("HttpService") local URL_ASTROS = "http://api.open-notify.org/astros.json" -- Make the request to our endpoint URL local response = HttpService:GetAsync(URL_ASTROS) -- Parse the JSON response local data = HttpService:JSONDecode(response) -- Information in the data table is dependent on the response JSON if data.message == "success" then print("There are currently " .. data.number .. " astronauts in space:") for i, person in pairs(data.people) do print(i .. ": " .. person.name .. " is on " .. person.craft) end end