Skip to content

Add support for multipart form data parsing#67

Open
outofcoffee wants to merge 1 commit into
mainfrom
claude/fix-formparams-multipart-uXTvE
Open

Add support for multipart form data parsing#67
outofcoffee wants to merge 1 commit into
mainfrom
claude/fix-formparams-multipart-uXTvE

Conversation

@outofcoffee
Copy link
Copy Markdown
Contributor

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 handled application/x-www-form-urlencoded requests, 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 calling ParseMultipartForm() when needed
    • isMultipartFormRequest(): Helper to detect multipart form requests
    • GetFormParams(): Returns a flat map of form parameters from both encoding types, handling only text fields and returning the first value for multi-valued parameters
    • GetFormValue(): Retrieves a single form parameter value, supporting both encoding types
  • Updated form parameter handling across the codebase:

    • internal/steps/script/context.go: Replaced manual form parsing with utils.GetFormParams()
    • internal/capture/capture.go: Replaced manual form parsing with utils.GetFormValue()
    • internal/template/template.go: Replaced manual form parsing with utils.GetFormValue()
  • Comprehensive test coverage:

    • Added unit tests for GetFormParams() and GetFormValue() covering URL-encoded, multipart, and empty body scenarios
    • Added integration tests in script execution, capture, and template processing to verify multipart form data works end-to-end

Implementation Details

The solution addresses a subtle issue with Go's http.Request.FormValue(): once ParseForm() is called (which handles URL-encoded data), the request's Form field becomes non-nil, preventing FormValue() from automatically calling ParseMultipartForm(). The parseRequestForms() 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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants