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.
Send an HTTP GET request, blocking the current thread until a response is received. If the HTTP response code is not in 200 class of status codes, this function raises an error. 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.
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
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