Http 400 on discord webhook

I’ve been working on a discord webhook system, but I am struggling when posting the webhook to the URL.

It seems that I am getting http 400 errors whenever trying to post to discord, I have searched but none of the results fix my problem. I assume I am formatting something incorrectly. Here is what I have:

		local webhookURL = "nolol"
		local payload = {
			content = "New Data Issue:",
			embeds = { {
				title = "Data Error",
				description = "A new data error has been detected for the following user: /n /n"..player.UserId,
				color = 16734296,
				fields = { {
					name = "Error Message",
					value = err
				}, {
					name = "Current Data",
					value = _G.dataTable
				} },
				author = {
					name = player.Name
				}
			} }
		}
	
		local json = HttpService:JSONEncode(payload)
		HttpService:PostAsync(webhookURL, json, Enum.HttpContentType.ApplicationJson)

Am I doing something incorrectly? Am I not formatting this right? I think I may have oversaw something so it’s probably pretty simple, I would like some help finding it though.

Thank you!

As far as I know, the embeds and fields are supposed to be arrays, at least when POSTing the webhook request.

Interesting. I know that the raw JSON is supposed to look like this:

{
    "content": "New Data Issue:",
    "embeds": [
      {
        "title": "Data Error",
        "description": "A new data error has been detected for the following user:\n\n$username",
        "color": 16734296,
        "fields": [
          {
            "name": "Error Message",
            "value": "$error"
          },
          {
            "name": "Current Data",
            "value": "$data"
          }
        ],
        "author": {
          "name": "$username"
        }
      }
    ]
  }

Do you mind sharing how I should format the Lua?

This post might help you.

1 Like

Unfortunately I am still having this issue. Here is my new code:

local payload = {
			["content"] = "New Data Issue:",
			["embeds"] = { {
				["title"] = "Data Error",
				["description"] = "A new data error has been detected for the following user: /n /n"..player.UserId,
				["color"] = tonumber(0xFF5858),
				["fields"] = { {
					["name"] = "Error Message",
					["value"] = err
				}, {
					["name"] = "Current Data",
					["value"] = _G.dataTable
				} },
				["author"] = {
					["name"] = player.Name
				}
			} }
		}
	
		local json = HttpService:JSONEncode(payload)
		HttpService:PostAsync(webhookURL, json, Enum.HttpContentType.ApplicationJson)

Can you print the string.len() of the field values? Also, adding the inline property to the array couldn’t hurt.

I’ve never used string.len(), how should I go about using it in this situation?

Are http requests on? That could be causing it.

When that webhook would execute, use

print(string.len(err))
print(string.len(_G.dataTable))

and share the results

1 Like

If HTTP Requests were off, there wouldn’t be an HTTP status code being returned.

Yes, they are.

Nothing seemed to print from the table but “121” printed from the err.

That could be why, you cannot send tables to Discord to post in an embed, strings only.
You’ll need to either delete that field or use JSONEncode to change the table to a string (essentially).

The max amount of characters you can have in a field value is 1024, and if you exceed that, or in your case provide a non-string, you will get 400 Bad Request.

Discord’s docs on embeds: Discord Developer Portal
Discord embed limitations: Discord Developer Portal

1 Like