Having issues sending to API: unable to cast string to token

Hello Im working on an admin system that has web logs and I’m getting an error when it sends a request to an endpoint
unable to cast string to token

the func: sendDataToServer is where its erroring.

code:

local api = {}

local Config = require(script.Parent.Parent.Config)

function api.sendDataToServer(endpoint, data)
	local url = Config.APIUrl .. endpoint;

	local success, response = pcall(function()
		return game.HttpService:PostAsync(url, "POST", data, true)
	end)

	if success then
		print("Data sent successfully:", response)
	else
		warn("Error sending data:", response)
	end
end

function api.getStatus()
	local endpoint = "/status"
	local url = Config.APIUrl .. endpoint

	local success, response = pcall(function()
		return game.HttpService:GetAsync(url, true)
	end)

	if success then
		print("Status retrieved successfully:", response)
		return response
	else
		warn("Error retrieving status:", response)
		return nil
	end
end


return api


2 Likes

Are you sure the data parameter in api.sendDataToServer() is an HttpContentType Type? The third parameter in PostAsync requires this Enumerator object, not a string value. If the data parameter is correct, maybe you should use Enum.HttpContentType[data]

2 Likes

Thanks I’ll try this and let you know.

1 Like

Error sending data: {"timestamp":1707021984,"userId":629627855,"reason":""} is not a valid member of "Enum.HttpContentType" - Server - Api:15 23:46:26.607 Infinite yield possible on 'cloud_218303841.Model.GUIs.SignalGUI.Scripts:WaitForChild("Mode")

	local url = Config.APIUrl .. endpoint;

	local success, response = pcall(function()
		return game.HttpService:PostAsync(url, "POST", Enum.HttpContentType[data], true)
	end)

	if success then
		print("Data sent successfully:", response)
	else
		warn("Error sending data:", response)
	end
end
1 Like

Oh now I see the problem. You accidentally swapped the "POST" and the data arguments together. Data is supposed to be the 2nd argument. However, I don’t see that POST is a valid item in HttpContentType. You should change that.

1 Like

Ok one second, let me update that.

1 Like

Think i might be doing this wrong its 12 AM for me

Error sending data: {“timestamp”:1707022315,“userId”:629627855,“reason”:“test”} is not a valid member of “Enum.HttpContentType”

function api.sendDataToServer(endpoint, data)
	local url = Config.APIUrl .. endpoint;

	local success, response = pcall(function()
		return game.HttpService:PostAsync(url, Enum.HttpContentType[data], true)
	end)

	if success then
		print("Data sent successfully:", response)
	else
		warn("Error sending data:", response)
	end
end

1 Like

My first response in the topic was wrong. You should instead do:

return game.HttpService:PostAsync(url, data, Enum.HttpContentType.ApplicationJson, true) -- ApplicationJson is the default value, but you can change it.
1 Like

Let me update this. and i’ll respond with the result.

1 Like

it sends no data and no logs to the console and server.

image
image
obv its not on localhost its on its own url

1 Like

It works on the roblox side its a server bug

1 Like