Calling the GetAsync function sends an HTTP GET request to the specified URL. Your script will yield until either a) the request is completed or b) the request times out. A timeout happens when a server goes a certain amount of time without a response.
If the HTTP response code is not in 200 class of status codes, this function raises an error.
If the request is cached, it will return old data from a previous request instead of fetching new data from the target address. If your data changes frequently, or accuracy is important, consider setting the
nocache argument to true.
A useful endpoint that can help you debug GET request is https://httpbin.org/get. It provides a JSON response with information about a GET request, such as headers and URL arguments.
See here for a detailed guide on
HttpService/PostAsync|sending and retrieving data via HTTP requests.
The web address you are requesting data from
Whether the request stores (caches) the response
Tells the server the return format of the data being requested
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