-
What do you want to achieve? Keep it simple and clear!
I want to consistently fetch API data without encountering the “HttpError: InvalidRedirect” error. -
What is the issue? Include screenshots / videos if possible!
Sometimes the API request works fine, but other times it fails with “HttpError: InvalidRedirect”. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
-
I tried adding retry logic, checking the URL, and using
pcall
to handle errors. I also checked the Developer Hub for API documentation but didn’t find a clear solution.
The script fetches game data from the RoProxy API. It works fine most of the time, but occasionally triggers an “InvalidRedirect” error, preventing the request from completing. I’ve tried adding retry logic, but the issue still occurs sometimes.
Script:
-- -- ServerScript
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getEventOne = ReplicatedStorage:WaitForChild("FetchDifferentGamesOne(1)")
local setEventOne = ReplicatedStorage:WaitForChild("FetchDifferentGames(1)")
-- Function to safely perform HTTP requests, retrying in case of invalid redirects
local function safeGetAsync(url)
local success, response = pcall(function()
return HttpService:GetAsync(url)
end)
if success then
return response
else
-- Handle failed request or invalid redirect
warn("Failed to fetch data from URL: " .. url .. ". Error: " .. response)
return nil
end
end
-- Function to get Place Info from the API (via RoProxy)
local function getPlaceInfo(placeId)
if not tonumber(placeId) then
return nil
end
-- Request the place info from RoProxy API
local url = "https://apis.roproxy.com/universes/v1/places/" .. placeId .. "/universe"
local response = safeGetAsync(url)
if response then
-- Decode the response and extract needed data
local data = HttpService:JSONDecode(response)
local universeId = data["universeId"]
local placeName = game:GetService("MarketplaceService"):GetProductInfo(placeId).Name
return {placeName, universeId}
else
-- Handle failed request or invalid redirect
getEventOne:Fire(controllingData)
-- Reset placeId to trigger a retry via setEventOne
placeId = nil
setEventOne.Event:Connect(function(gameID)
placeId = gameID
getPlaceInfo(placeId) -- Retry after receiving the new game ID
end)
-- Wait until a valid placeId is received
while placeId == nil do
wait(0.01)
end
end
end