-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Enable encodeUrl setting to control URL encoding in generated snippets #7187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,22 @@ import { getAllVariables, getTreePathFromCollectionToItem, mergeHeaders } from ' | |
| import { resolveInheritedAuth } from 'utils/auth'; | ||
| import { get } from 'lodash'; | ||
| import { interpolateAuth, interpolateHeaders, interpolateBody, interpolateParams } from './interpolation'; | ||
| import { parse, format } from 'url'; | ||
| import { stringify } from 'querystring'; | ||
|
|
||
| const getEncodedUrl = (rawUrl) => { | ||
| const parsed = parse(rawUrl, true, true); | ||
| if (!parsed.query || Object.keys(parsed.query).length === 0) { | ||
| return rawUrl; | ||
| } | ||
| const search = stringify(parsed.query); | ||
| return format({ | ||
| ...parsed, | ||
| search, | ||
| query: parsed.query, | ||
| path: search ? `${parsed.pathname}?${search}` : parsed.pathname | ||
| }); | ||
| }; | ||
|
|
||
| const addCurlAuthFlags = (curlCommand, auth) => { | ||
| if (!auth || !curlCommand) return curlCommand; | ||
|
|
@@ -79,6 +95,17 @@ const generateSnippet = ({ language, item, collection, shouldInterpolate = false | |
| result = addCurlAuthFlags(result, effectiveAuth); | ||
| } | ||
|
|
||
| // Respect encodeUrl setting: when not explicitly true, replace HTTPSnippet's encoded URL with the raw URL | ||
| // encodeUrl defaults to false in the UI when undefined/null | ||
| const settings = item.draft ? get(item, 'draft.settings') : get(item, 'settings'); | ||
| if (settings?.encodeUrl !== true) { | ||
| const rawUrl = request.url; | ||
| const encodedUrl = getEncodedUrl(rawUrl); | ||
| if (encodedUrl !== rawUrl) { | ||
| result = result.replaceAll(encodedUrl, rawUrl); | ||
| } | ||
| } | ||
|
||
|
|
||
| return result; | ||
| } catch (error) { | ||
| console.error('Error generating code snippet:', error); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sanish-bruno, Found these consistent issues:
Spaces stay encoded even when encodeUrl is false
For example: https://example.com/api?search=hello world
Even with
encodeUrl: false, the generated snippet still contains %20 instead of preserving the space as written.Hash fragment lost in URLs with #
For example: https://example.com/api?token=abc==#section
With
encodeUrl: false, the #section fragment is not preserved correctly and gets lost in the generated snippet.