Issues using HttpService

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? :frowning:

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)
1 Like

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.

Here is my version of your code

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()
1 Like

The problem is quite simple.

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 :.

2 Likes

the p call has to be in a function otherwise it wont work

Another reason why it may not work is because Roblox recently restricted some of the ports for HttpService.

Here’s the full announcement:

https://devforum.roblox.com/t/port-restrictions-for-httpservice/

1 Like

This is using the default :443 port which is allowed.

Hello! What do you mean? to write comments in the code or something like that? :confused:

omg! rlly? are you serious? :cold_sweat:

1 Like

Wow thank you very much! This works! :smiley: