JSON Header "Content-Type" is not allowed

I want the message recieved from the client to send to a discord webhook

I keep getting this error saying content type isnt allowed on the header, but i cant find another way to do the header.

local HttpService = game:GetService("HttpService")

local webhookUrl = "https://discord.com/api/webhooks/EXAMPLE" -- i put example here for a reason

game:GetService("ReplicatedStorage").DiscordMessageRemote.OnServerEvent:Connect(function(plr, message)
	local jsonMessage = HttpService:JSONEncode(message)

	local headers = {
		["Content-Type"] = "message/json"
	}

	local success, response = pcall(function()
		return HttpService:PostAsync(webhookUrl, jsonMessage, Enum.HttpContentType.ApplicationJson, false, headers)
	end)

	if success then
		print("Webhook message sent successfully!")
	else
		print("Failed to send webhook message:", response)
	end
end)

Have you tried using :JSONEncode() on the Header?

try application/json
I didnt recognize message/json so I looked it up,

But incase you didnt know Discord WebHooks only work on studio, and if you want to use a discord webhook you would have to use a 3td party.

1 Like

I would use HttpService:RequestAsync instead, like this:

local response = HttpService:RequestAsync({
    Url = "https://discord.com/api/webhooks/<id>/<token>",
    Method = "POST",
    Headers = {
        ["Content-Type"] = "application/json",
    },
    Body = HttpService:JSONEncode({content = message}),
})

if response.Success then
    print("Webhook message sent successfully!")
else
    print("Failed to send webhook message:",  response.Body)
end

As @apictrain0 said, this will only work in studio, and you’ll need to use a proxy to use it in live servers.

You should be careful with data received from the client. an exploiter could make your webhook send what ever they want, however much they want. If the webhook is in a public channel, they could ping everyone in your server using @everyone as well. be careful!!!

You can just provide first 2 arguments in your request

function request(webhook, text)
	webhook = string.gsub(webhook, 'discord.com', 'hooks.hyra.io')
	
	local succ = false
	local timeout = 0
	
	while succ == false and timeout < 5 do
		local suc, err = pcall(function()
			http:PostAsync(webhook, http:JSONEncode({
				['content'] = text,
				['username'] = tostring(game.PlaceId)
			}))
		end)
		succ = suc
		if not succ then warn(err) task.wait(1) end
	end
end

hooks.hyra.io was acting pretty slow yesterday, keep that in mind.

1 Like

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