Need help for HTTP Request

Hello devs!
I need some help, how can I return the value of the request status? (200 for example) I read the documentation but I didn’t found nothing. :confused:

Here is my actual code:

local httpService = game:GetService('HttpService')

local function request()
	local response
	
	pcall(function()
		response = httpService:GetAsync("https://economia.awesomeapi.com.br/last/USD-BRL")
	end)
	
	if response then
		local data = httpService:JSONDecode(response) -- where I translate my JSON file
		return data['USDBRL']['bid']
	end
end

print(request()) -- calling my func
1 Like

Pretty simple actually, you just need to use HttpsService:RequestAsync() instead of get.
This will give you the header with the status code attached with it.

local httpService = game:GetService('HttpService')

local function request()
	local response
	
	pcall(function()
		response = httpService:RequestAsync({
			Url = "https://economia.awesomeapi.com.br/last/USD-BRL";
			Method = "GET";
			})
	end)
	
	--[[
		response will now be structured as:
		{
			["Body"] = data we want from the site
			["Headers"] = any extra cloudflare headers, statuses, endpoints, etc.
			["StatusCode"] = the code that we are after
			["StatusMessage"] = the code attached to the message
			["Success"] = a boolean that tells us if we were able to do the request or not (obviously)
		}
	
	]]
	print("Response Code: "..response.StatusCode)
	
	if response then
		local data = httpService:JSONDecode(response["Body"]) -- use body for the data, as it contains the information we actually want
		return data['USDBRL']['bid']
	end
end

print(request())
1 Like

It worked, thank you so much! :grin:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.