[PR #32317] fix: json body corruption when string variables contain special characters #33667

Open
opened 2026-02-21 20:53:41 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/32317

State: open
Merged: No


When the HTTP Request node builds a JSON body from a template like {"content": {{#node.result#}}}, it was doing raw string substitution via convert_template().text and then running repair_json on the result. If the variable value contains quotes, backslashes, newlines, or pipe characters (common in markdown), the substitution breaks the JSON structure and repair_json corrupts the data rather than fixing it.

The root cause is that string values weren't being JSON-escaped before substitution. For example, a value like hello "world" would produce {"content": hello "world"} — broken JSON that repair_json can't reliably reconstruct.

Fixed by adding _convert_template_for_json() which uses json.dumps() to properly encode string variable values before they land in the JSON structure. Numbers, objects, and arrays continue to use their existing .text representation which already produces valid JSON tokens. Also handles the edge case where template authors already wrap variables in quotes (e.g. "{{#var#}}").

Added two regression tests covering strings with quotes/backslashes/newlines and markdown table content.

Closes #31927

**Original Pull Request:** https://github.com/langgenius/dify/pull/32317 **State:** open **Merged:** No --- When the HTTP Request node builds a JSON body from a template like `{"content": {{#node.result#}}}`, it was doing raw string substitution via `convert_template().text` and then running `repair_json` on the result. If the variable value contains quotes, backslashes, newlines, or pipe characters (common in markdown), the substitution breaks the JSON structure and `repair_json` corrupts the data rather than fixing it. The root cause is that string values weren't being JSON-escaped before substitution. For example, a value like `hello "world"` would produce `{"content": hello "world"}` — broken JSON that `repair_json` can't reliably reconstruct. Fixed by adding `_convert_template_for_json()` which uses `json.dumps()` to properly encode string variable values before they land in the JSON structure. Numbers, objects, and arrays continue to use their existing `.text` representation which already produces valid JSON tokens. Also handles the edge case where template authors already wrap variables in quotes (e.g. `"{{#var#}}"`). Added two regression tests covering strings with quotes/backslashes/newlines and markdown table content. Closes #31927
yindo added the pull-request label 2026-02-21 20:53:41 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33667