Add support for multipart form data parsing#67
Open
outofcoffee wants to merge 1 commit into
Open
Conversation
Go's ParseForm only handles application/x-www-form-urlencoded bodies, so formParams was empty for multipart requests in script context, capture and template lookups. Add shared utils.GetFormParams / utils.GetFormValue helpers that also call ParseMultipartForm when the content type is multipart/form-data, and route the three call sites through them. Fixes #65
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.
Summary
This PR adds comprehensive support for parsing multipart form data (
multipart/form-data) in addition to the existing URL-encoded form support. Previously, the application only handledapplication/x-www-form-urlencodedrequests, which limited form parameter extraction in multipart scenarios.Key Changes
New utility functions in
pkg/utils/http.go:parseRequestForms(): Ensures both URL-encoded and multipart form bodies are parsed by explicitly callingParseMultipartForm()when neededisMultipartFormRequest(): Helper to detect multipart form requestsGetFormParams(): Returns a flat map of form parameters from both encoding types, handling only text fields and returning the first value for multi-valued parametersGetFormValue(): Retrieves a single form parameter value, supporting both encoding typesUpdated form parameter handling across the codebase:
internal/steps/script/context.go: Replaced manual form parsing withutils.GetFormParams()internal/capture/capture.go: Replaced manual form parsing withutils.GetFormValue()internal/template/template.go: Replaced manual form parsing withutils.GetFormValue()Comprehensive test coverage:
GetFormParams()andGetFormValue()covering URL-encoded, multipart, and empty body scenariosImplementation Details
The solution addresses a subtle issue with Go's
http.Request.FormValue(): onceParseForm()is called (which handles URL-encoded data), the request'sFormfield becomes non-nil, preventingFormValue()from automatically callingParseMultipartForm(). TheparseRequestForms()helper explicitly calls both parsing methods to ensure all form data is available regardless of content type.Form parameters are deduplicated (URL-encoded takes precedence) and only the first value is returned for multi-valued parameters, providing a simple string-based interface for template and script contexts.
https://claude.ai/code/session_014vwtKZV7ZX1YGGTzZ6UcMQ