fix(net/gclient): fix form field value truncation when uploading files#4627
Merged
hailaz merged 1 commit intogogf:masterfrom Jan 19, 2026
Merged
Conversation
gogf#4156) When posting form data with file upload, if a field value contains '=' or '&', the value was being truncated. This was caused by: 1. Global URL encoding was disabled when any value contained '@file:' marker 2. strings.Split(item, "=") split on ALL '=' characters in the value 3. URL-encoded values were not decoded before writing to multipart form This fix: - Removes the global URL encoding disable logic - Uses strings.SplitN(item, "=", 2) to only split on the first '=' - Adds URL decoding for field values before writing to multipart form - Keeps '@file:' prefixed values unencoded for file upload detection
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where form field values containing = or & characters were being truncated when uploading files via multipart form data.
Changes:
- Modified parameter parsing to use
SplitNwith limit 2 to only split on the first=character - Added URL decoding for form field names and values in multipart requests
- Removed global URL encoding disable logic that was causing the truncation issue
- Added comprehensive test coverage for various edge cases
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| net/gclient/gclient_z_unit_issue_test.go | Added 7 comprehensive test functions covering the fix and edge cases including special characters, multiple fields, pre-encoded values, and various scenarios |
| net/gclient/gclient_request.go | Changed Split to SplitN for limiting splits and added URL decoding for field names and values; imported gurl package for decoding |
| internal/httputil/httputils_test.go | Added 5 test functions to verify URL encoding behavior, file upload marker handling, and parameter building |
| internal/httputil/httputils.go | Removed global URL encoding disable logic and simplified @file: marker detection; removed unused gstr import |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
hailaz
approved these changes
Jan 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #4156
When posting form data with file upload, if a field value contains
=or&, the value was being truncated.Example
Expected: Server receives
fieldName = "aaa=1&b=2"Actual (before fix): Server receives
fieldName = "aaa"(truncated)Root Cause Analysis
The issue was caused by three problems in the original code:
Problem 1: Global URL encoding disable (httputils.go)
When any value contained
@file:, URL encoding was disabled for ALL values, causing"aaa=1&b=2"to remain unencoded. The&character was then treated as a parameter separator.Problem 2: Split on all
=characters (gclient_request.go)This caused
"fieldName=aaa=1"to be split into["fieldName", "aaa", "1"].Problem 3: No URL decoding for field values
URL-encoded values were written directly to the multipart form without decoding.
Solution
Fix 1: Remove global URL encoding disable
Only
@file:prefixed values are kept unencoded for file upload detection. Other values are properly URL-encoded.Fix 2: Use SplitN to limit split count
Fix 3: Add URL decoding for field values
Compatibility Analysis
=or&@file:(no path)@file:but file doesn't exist"aaa%3D1"Breaking Change Assessment
No breaking changes. The fix only affects the file upload scenario where field values contain special characters (
=,&). Previously this scenario was broken, now it works correctly.Edge Cases
Literal
@file:value: GoFrame treats@file:as a special marker for file upload. This is a framework design decision and remains unchanged.URL decode failure: If URL decoding fails (e.g., invalid
%XXsequence), the original value is preserved.Test Coverage
Added comprehensive tests covering:
Test_Issue4156- Basic fix verificationTest_Issue4156_MultipleSpecialChars- Multiple=,&,%,+, spacesTest_Issue4156_MultipleFields- Multiple fields with special charactersTest_Issue4156_NoFileUpload- Normal POST without file uploadTest_Issue4156_PreEncodedValue- Pre-encoded values like%3DTest_Issue4156_EmptyAndSpecialValues- Edge cases (=at start/end, only special chars)TestBuildParams_*- httputil.BuildParams comprehensive testsAll tests pass, including existing
Test_Issue3748which tests the@file:marker handling.Files Changed
internal/httputil/httputils.go- Remove global URL encoding disable, adjust@file:conditioninternal/httputil/httputils_test.go- Add comprehensive BuildParams testsnet/gclient/gclient_request.go- Use SplitN, add URL decodingnet/gclient/gclient_z_unit_issue_test.go- Add Issue 4156 test cases