mirror of
https://github.com/hoppscotch/hoppscotch.git
synced 2026-04-26 01:06:00 +03:00
[PR #5894] fix(common): escape special characters in generated code snippets #5400
Labels
No labels
CodeDay
a11y
browser limited
bug
bug fix
cli
core
critical
design
desktop
discussion
docker
documentation
duplicate
enterprise
feature
feature
fosshack
future
good first issue
hacktoberfest
help wanted
i18n
invalid
major
minor
need information
need testing
not applicable to hoppscotch
not reproducible
pull-request
question
refactor
resolved
sandbox
self-host
spam
stale
testmu
wip
wont fix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/hoppscotch#5400
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/hoppscotch/hoppscotch/pull/5894
Author: @danielalanbates
Created: 2/20/2026
Status: 🔄 Open
Base:
main← Head:fix/issue-3011📝 Commits (1)
cbed4c9fix(common): escape special characters in generated code snippets (#3011)📊 Changes
1 file changed (+69 additions, -2 deletions)
View changed files
📝
packages/hoppscotch-common/src/helpers/new-codegen/har.ts(+69 -2)📄 Description
Summary
Fixes #3011
When a JSON request body contains values with special characters (newlines
\n, tabs\t, backslashes\\, etc.), the Generate Code feature produces syntactically broken output for several target languages. For example, a Python requests snippet might embed a literal newline character inside a string literal instead of using the\nescape sequence.Root cause
The code generation pipeline builds a HAR request object and passes it to
@hoppscotch/httpsnippet. For JSON bodies, httpsnippet internally callsJSON.parse()on the body text — converting escape sequences like\ninto actual control characters. Some code generators (Pythonrequests, Rustreqwest, PHPcurl, Swiftnsurlsession, Objective-Cnsurlsession) then embed these control characters literally into the generated code without re-escaping them.Fix
In
buildHarPostData(packages/hoppscotch-common/src/helpers/new-codegen/har.ts):jsonHasProblematicChars()— recursively checks whether a parsed JSON body contains string values with control characters or backslashespostData.mimeTypeis set to"text/plain"instead of"application/json". This prevents httpsnippet from parsing the body into ajsonObjand forces generators to use the raw text code path, which handles escaping correctly viaJSON.stringify()or equivalentContent-Typeheader is preserved viabuildHarHeaders(), so the generated code still sendsapplication/jsonapplication/jsonpath for idiomatic generated code (e.g. Python'sjson=payload)Before / After
Body:
{"content": "a=1\nprint(a+20)"}Before (broken Python):
After (valid Python):
Test plan
json=payloadpath\nin a string value, generate code in Python requests, verify the output is syntactically validThis PR was created with the assistance of Claude Opus 4.6 by Anthropic. Happy to make any adjustments! Reviewed and submitted by a human.
Summary by cubic
Fixes broken generated code when JSON bodies contain special characters (e.g., \n, \t, \) across targets like Python, Rust, PHP, Swift, and Objective-C. Detects such bodies and uses the raw text path so characters are correctly escaped while preserving Content-Type: application/json.
Written for commit
cbed4c9665. Summary will update on new commits.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.