I’ve recently started playing around with HTTPSservice and it doesn’t seem to be working the way I want it to.
I’ve been trying to get a random fact from this specific link (API) http://api.fungenerators.com
But it doesn’t return a fact, it returns something totally random:
I’m unsure what I’ve done wrong. My code is this:
local randomFactsURL = "http://api.fungenerators.com"
local httpService = game:GetService("HttpService")
local function FetchFacts()
local response
local success, errormessage = pcall(function()
response = httpService:RequestAsync(
{
Url = randomFactsURL,
Method = "GET",
Headers = {
["Content-Type"] = "application/json"
}
}
)
end)
if success then
if response.Success then
print(response.Body)
end
else
warn(errormessage)
end
end
FetchFacts()
Just looking at the API itself you need to pass some parameters like seen in this screenshot:
Another this is that I’m pretty sure you need a API token to actually use their API so make sure you do that as well.
The thing is, nothing is being returned when I try it. My current code is:
local randomFactsURL = "http://api.fungenerators.com/fact/random?category=Countries&subcategory=USA?X-Fungenerators-Api-Secret=444777"
local httpService = game:GetService("HttpService")
local function FetchFacts()
local response
local success, errormessage = pcall(function()
response = httpService:RequestAsync(
{
Url = randomFactsURL,
Method = "GET",
Headers = {
["Content-Type"] = "application/json",
}
}
)
end)
if success then
if response.Success then
print(response.Body)
else
print("Nothing returned")
end
else
warn(errormessage)
end
end
FetchFacts()
Do you know the concept of variables inside functions and outside functions? You need to add the if statement inside the function to make it work I guess.
Edit: I do know this is in Python, I do not know the exact concept in Luau.
No, the if success then is outside of the function as you can clearly see end. Here’s what you need to do. Add the if else statement inside the function.
local randomFactsURL = "http://api.fungenerators.com/fact/random?category=Countries&subcategory=USA?X-Fungenerators-Api-Secret=444777"
local httpService = game:GetService(“HttpService”)
local function FetchFacts()
local response
local success, errormessage = pcall(function()
response = httpService:RequestAsync(
{
Url = randomFactsURL,
Method = “GET”,
Headers = {
[“Content-Type”] = “application/json”,
}
print(response.Body)
else
print(“Nothing returned”)
end
else
warn(errormessage)
}
)
end)
if success then
if response.Success then