I have started to use various api’s from api.roblox, but I see that there are parameters that can be entered. For example, I was trying to use this API:
I am not too sure where I would include the userid in the parameter, because with other APIs, you put in certain information into the url, but it doesnt look like there is a place in there to put the URL.
Also, I am using NexusAvenger’s tutorial so that I can access api.roblox from google scripts. This is the script I am running. It does require a module, but you can find that in NexusAvenger’s tutorial.
local HttpProxy = require(script.Parent.HttpProxyService:Clone())
local myURL = "https://api.roblox.com/userblock/block"
local Response = game.HttpService:JSONEncode(HttpProxy:PostAsync(myURL))
local data = game.HttpService:JSONDecode(Response)
if data.success then
print("Successfuly blocked")
end
I tried to put the userid in the PostAsync after myURL, but that did not work, and I get an error of attempting to call a nil value.
Oh never mind, I guess you’re using a module to do it somehow. I don’t have much to say I guess since I have not idea what the scripts behind that module are, nor do I know what the screenshot you sent is supposed to mean. You put parameters into functions, not links.
Oh yeah, the request is being sent to google scripts, and then it sends it to the api.roblox. Yeah, I wish they would make it a little clearer where they go, but
I’m slightly confused by you encoding then immediately decoding the same info - what’s the purpose of that bit of logic?
If there’s no place in the URL then I would assume it’s sent as a parameter in the post body, though it’s possible that they just missed it from the documentation.
What error specifically did you get from the call itself (not Roblox, where you called a nil value - though this implies a logic error where you don’t handle API errors appropriately). I imagine NexusAvenger’s module returns error info in one form or another.
Oh, I really had no purpose with encoding and then decoding. With NexusAvenger’s script, it was all one line, so I took it apart some. I guess I had combined what the devhub had used and combined it with this one.
And to answer your second question, there is no error provided here from the module script, but it does return errors. I think before it can even send it to the google script it errors, hence why there’s no error coming from the module script.
Oh, and to @FKAGamingDeOne I had tried that shortly after posting the question, and it didn’t work. I just tried again and I’m still getting the error that’s listed above.
It could just be the way I am using PostAsync though…
Did you call game:GetService( 'HttpService' ) at any point? If not then that’s probably the issue. Some services require you to create them, that’s why it’s always best to use GetService as it’ll create the service if it doesn’t already exist.
Fix that line and you can begin working out whether the API works or not. And then you can try FKAGamingDeOne’s suggestion for the URL.
Yeah, I just did game:GetService(“HttpService”) and it did not work. And I did it with FKA’s link(with the changing USERID to an actual id), and still didn’t work. I am just going to put the module here as reference
local Url = "https://script.google.com/macros/s/" .. ScriptId .. "/exec"
local HttpService = game:GetService'HttpService'
local Module = {}
function JSONDecode(JSON)
local JSONTable = {}
pcall(function ()
JSONTable = HttpService:JSONDecode(JSON)
end)
return JSONTable
end
function Module:GetAsync(Link)
local JSON = HttpService:GetAsync(Url .. "?q="..game.HttpService:UrlEncode(Link))
local Result = JSONDecode(JSON)
if Result.result == "success" then
return Result.response
else
warn(tostring(Link).." failed to fetch because "..tostring(Result.error))
return
end
end
return Module
Could it be that I need to change the GetAsync in the module script to PostAsync? Oh, and ScriptId is defined, I just didn’t include it in there so no one can access the link.
Correct, in that PostAsync doesn’t exist in the module.
You’ll need to do a little bit more than just changing the function name - you’ll want to swap the method on the line inside the function to PostAsync too, and if you want to provide full functionality it’s probably best to allow postdata to be sent through it too.
For now, changing the name of the module function (or preferably copying the entire function and naming the copy PostAsync) and changing the first line inside it will be enough to get you going.
Provide an empty string as the second parameter to the HttpService:PostAsync call. Apparently it requires the data parameter even if you aren’t sending any postdata. It’s rare for a post call to not have data so I guess this parameter makes sure you did actually mean post and not get.
Okay, I’ve done what you said, and it says which is probably because the module couldnt pass an argument, so the server script can’t even get the first argument.
And just to show you what I have, this is the part of the module script
function Module:PostAsync(Link)
local data = {}
local JSON = HttpService:PostAsync(Url .. "?q="..HttpService:UrlEncode(Link),"")
local Result = JSONDecode(JSON)
if Result.result == "success" then
return Result.response
else
warn(tostring(Link).." failed to fetch because "..tostring(Result.error))
return
end
end
And this is the server script.
local HttpProxy = require(script.Parent.HttpProxyService:Clone())
local HttpService = game:GetService("HttpService")
local myURL = "https://api.roblox.com/userblock/block/?userId=747819869"
local Response = HttpService:JSONEncode(HttpProxy:PostAsync(myURL),"")
if Response.success then
print("Successfuly blocked")
end
The issue now is regarding the result from the API.
Unfortunately I can’t help much there, as I don’t know how the specific macro works that is being used here. All I can tell you is that the returning JSON object, does not have an error field (i.e. Result.error is nil) and its success field (if it exists) does not equal the string “success”.
Your best bet is to output the result before and after decoding to see what’s actually returned. Take a look at the keys and values, and hopefully that will give you some indication into what your conditional statements should be.
Looking at the java code in the google script, it seems like it is only usable for GetAsync(at least from what I understand and the name being doGet()). Anyways, thanks for helping out and giving your time.