Here, I am trying to get the JSON data shown here to replicate into my script via HttpService:RequestAsync().
Here is my code:
local HttpService = game:GetService("HttpService")
local r = HttpService:RequestAsync("https://api.roblox.com/marketplace/productinfo?assetId=99723382")
local i = HttpService:JSONDecode(r.Body)
for n, v in pairs(i) do
print(tostring(n)..", "..tostring(v))
end
Here is the error I recieve: Unable to cast to Dictionary
The error refers back to line 2, with the code
local r = HttpService:RequestAsync("https://api.roblox.com/marketplace/productinfo?assetId=99723382")
Any help with what I am doing wrong would be greatly appreciated
Answer: Read the documentation carefully. The RequestAsync function has the first parameter as a Dictionary, not as a string representing the Url.
local HttpService = game:GetService("HttpService")
local r = HttpService:RequestAsync({
Url = "https://api.roblox.com/marketplace/productinfo?assetId=99723382",
Method = "GET"
})
local i = HttpService:JSONDecode(r.Body)
for n, v in pairs(i) do
print(tostring(n)..", "..tostring(v))
end
Notes:
As seen when running the code, HttpService does not have access to ROBLOX resources.
Always wrap http requests with pcall(), as the function will throw an error when things go wrong (instead of “complaining”)