I’ve read through a couple posts about POST requests but none of them seemed to help. I’ve made an api, where a request would be like “https://url.com/check-user?id=123”, and I’m trying to get the response from the API. Here is the code I’ve tried so far.
local http = game:GetService("HttpService")
local id = 123
local url= 'https://url.com/check-user'
local data = {
["id"] = 123
}
local response = http:PostAsync(url, http:JSONEncode(data))
print(response)
I got the api to log what the “id” was, and it logs just {}. My API works off of roblox, but I’m trying to get it to work in roblox. Any help would be appreciated.
The ID value is not carried through, like I said before, the url being sent should be something like “https://url.com/check-user?id=123”, but that is not being sent through to the API.
local http = game:GetService("HttpService")
local id = 123
local url= 'https://url.com/check-user'
local response = http:GetAsync(url.."?id="..tostring(id))
print(response)
but if you have not found out the issue I have a solution! If you’re trying to receive JSON aka tables on on the proxy (and especially if you’re using express.js) You first have to URLEncode the table and then send the encoded data.
local http = game:GetService("HttpService")
local url= 'https://url.com/check-user'
local data = {
["id"] = 123
}
function UrlEncodeTable(table)
local data = ""
for k, v in pairs(table) do
data = data .. ("&%s=%s"):format(
game:GetService("HttpService"):UrlEncode(k),
game:GetService("HttpService"):UrlEncode(v)
)
end
return data
end
local response = http:PostAsync(url, UrlEncodeTable(data), Enum.HttpContentType.ApplicationUrlEncoded, false)
--If you are receiving another JSON from the response do
local GetInfo = http:PostAsync(url, UrlEncodeTable(data), Enum.HttpContentType.ApplicationUrlEncoded, false)
local response = http:JSONDecode(GetInfo)
print(response)