Hello, I am doing some tests with this service and it caused me an error, could someone give me a hand, what am I doing wrong?
local HttpService = game:GetService('HttpService')
local URL = 'https://pokeapi.co/api/v2/pokemon/1'
function httpRequest(getAsync, service, urlLink)
local success, errorMessage = pcall(getAsync, urlLink)
if not success then
warn('Something went wrong!')
return warn(errorMessage)
else
warn('Everything works perfect!')
local decodedTable = service:JSONDecode(success)
print(decodedTable)
end
end
httpRequest(HttpService:GetAsync(), HttpService, URL)
Hi! When making a request for help, please add information about what you have tried and the errors you are getting. This was in the template that you helpfully ignored.
local HttpService = game:GetService('HttpService')
local URL = 'https://pokeapi.co/api/v2/pokemon/1'
function httpRequest()
local response
local data
local success, errorMessage = pcall(function()
response = HttpService:GetAsync(URL)
data = HttpService:JSONDecode(response)
end)
if not success then
warn('Something went wrong!')
return warn(errorMessage)
else
warn('Everything works perfect!')
print(data)
end
end
httpRequest()
You can’t pass HttpService:GetAsync() through a function like this. You’re passing the result, not the actual method, which doesn’t work. It’s also completely pointless. In reality, you’re looking to do this:
local HttpService = game:GetService("HttpService")
...
local success, errorMessage = pcall(HttpService.GetAsync, HttpService, urlLink)
This calls the method inside of the pcall as it should. Note that you have to pass HttpService as a parameter, because you are calling the method with a dot . rather than a colon :.