Fix Discord Webhook Markdown Code Block Showing Plain Text
🔍 WiseChecker

Fix Discord Webhook Markdown Code Block Showing Plain Text

Discord webhooks are a powerful way to send automated messages from external services into your server channels. However, you may find that markdown code blocks—intended to display formatted code or monospaced text—appear as plain, unformatted text instead. This typically happens because the webhook payload does not include the correct Content-Type header or uses an unsupported formatting method. This article explains why code blocks fail to render and provides exact steps to fix the issue for both JSON payloads and third-party integrations.

Key Takeaways: Fixing Code Block Rendering in Discord Webhooks

  • Set Content-Type header to application/json: Ensures Discord interprets the payload as valid JSON and processes markdown correctly.
  • Use triple backticks with language identifier: Wrap code in “`language“` to trigger syntax highlighting and proper block rendering.
  • Escape backslashes in JSON strings: Double backslashes (\n) preserve newlines inside code blocks; otherwise they become literal \n text.

ADVERTISEMENT

Why Discord Webhooks Display Code Blocks as Plain Text

Discord webhooks accept JSON payloads that contain a content field. When you include triple backticks (```) in that field, Discord’s message parser should convert them into a styled code block. If the code block shows as plain text, the root cause is almost always one of three things:

Incorrect Content-Type Header

The webhook endpoint expects the request to have a Content-Type header set to application/json. If you send the payload as text/plain or application/x-www-form-urlencoded, Discord treats the entire body as raw text and does not parse markdown. The code block delimiters become literal characters.

Improper JSON Escaping

Inside a JSON string, backslashes must be escaped. If your code block contains backslashes (common in Windows paths or regex), you need to write \\ instead of \. Additionally, newlines inside the content field must be represented as \n — not actual line breaks in the JSON body. If you use literal newlines, the JSON is malformed and Discord may ignore the markdown.

Third-Party Service Formatting

Services like GitHub, GitLab, or monitoring tools send webhooks using their own payload structure. Some services wrap code in HTML <pre> tags or use a different markdown flavor. Discord does not render HTML; it only processes Discord-flavored markdown. If the service sends <pre>code</pre>, you will see the tags as plain text.

Steps to Fix Code Block Rendering in Discord Webhooks

Follow these steps to ensure your webhook payload correctly renders code blocks. The instructions apply to any HTTP client (curl, Postman, or a custom script).

  1. Set the Content-Type header to application/json
    When sending the request, include the header Content-Type: application/json. Without this, Discord treats the body as plain text and ignores markdown. In curl, use -H "Content-Type: application/json". In Postman, select the “Body” tab, choose “raw”, and set the format to “JSON”.
  2. Wrap code in triple backticks with a language identifier
    Inside the content field, write your code block as:
    ```python\nprint("Hello")\n```
    The language identifier (e.g., python, javascript, bash) triggers syntax highlighting. Without it, the block still renders as monospaced text but without colors. Always include a newline (\n) after the opening backticks and before the closing backticks.
  3. Escape backslashes and newlines in the JSON string
    In JSON, every backslash must be doubled. For example, a Windows path C:\Users\Name becomes C:\\Users\\Name. Newlines must be written as \n — not actual line breaks. A correct payload looks like:
    {"content": "```\nC:\\Users\\Name\n```"}
  4. Test with a minimal payload using curl
    Run this command in a terminal, replacing WEBHOOK_URL with your webhook URL:
    curl -H "Content-Type: application/json" -X POST -d '{"content":"```\nHello\n```"}' WEBHOOK_URL
    If the message shows a code block with “Hello”, the fix works. If not, double-check the header and escaping.
  5. If using a third-party service, modify the payload before sending
    If you cannot change the service’s outgoing webhook, use a middleware service like Zapier, Make, or a custom serverless function to transform the payload. For example, replace <pre>...</pre> with ```...``` and set the correct Content-Type header.

ADVERTISEMENT

If Discord Webhooks Still Show Plain Text After the Main Fix

Webhook URL Contains a Trailing Slash or Extra Characters

A malformed webhook URL can cause Discord to reject the payload or ignore formatting. Ensure the URL ends exactly with /slack or /github depending on the integration type. For custom webhooks, the URL should look like https://discord.com/api/webhooks/123456/abc123 with no extra slashes or query parameters.

Payload Exceeds Character Limits

Discord limits a webhook message to 2000 characters. If your code block pushes the total over this limit, Discord truncates the message and may strip the closing backticks, causing the block to appear as plain text. Split long output into multiple webhook calls or use the embeds field, which allows up to 6000 characters per embed.

Using the Wrong Webhook Type (Slack-Compatible vs Custom)

Discord supports Slack-compatible webhooks at the endpoint /slack. If you send a Slack-formatted payload to a custom webhook (or vice versa), markdown may not parse correctly. For custom webhooks, always use the content field. For Slack-compatible webhooks, use the text field. Check your webhook URL: if it ends with /slack, use Slack format.

Webhook Payload Formats: Custom vs Slack-Compatible

Item Custom Webhook Slack-Compatible Webhook
Endpoint suffix None (plain webhook URL) /slack appended to webhook URL
Main field for content content text
Markdown support Full Discord markdown Slack markdown (limited)
Code block syntax “`language“` “`language“` (same, but may require mrkdwn_in: ["text"])
Content-Type header application/json application/json

Discord webhooks that display code blocks as plain text are almost always caused by a missing or incorrect Content-Type header, improper JSON escaping, or a third-party service sending unsupported formatting. By setting the header correctly, using triple backticks with a language identifier, and escaping backslashes and newlines, you can restore proper code block rendering. For persistent issues, check the webhook URL type and character limits. As an advanced tip, use Discord’s embed object with the description field to include longer code snippets without breaking markdown.

ADVERTISEMENT