Skip to content

Fix multipart array value parsing in HTTP client (#55732) #56302

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

Merged
merged 2 commits into from
Jul 18, 2025

Conversation

joteejotee
Copy link
Contributor

Description

Fixes an issue where the HTTP client would throw InvalidArgumentException: A 'contents' key is required. when sending multipart/form-data requests containing both file attachments and array values (e.g., checkbox selections).

Problem

The parseMultipartBodyFormat method in PendingRequest was unable to handle array values correctly when formatting multipart data. Array values like ['Network Administrator', 'Janitor'] would be passed through as-is instead of being converted to the proper multipart format.

Solution

  • Changed map() to flatMap() to properly expand array values
  • Array values are now formatted as individual field[] entries for each value
  • Maintains backward compatibility with existing name/contents format
  • Added comprehensive tests for the new functionality

Changes

  • Core Fix: Updated parseMultipartBodyFormat() method in PendingRequest.php
  • Tests: Added test cases for array values and mixed file+array scenarios

Testing

  • ✅ Array values are correctly formatted as field[] entries
  • ✅ Backward compatibility maintained for existing multipart format
  • ✅ File uploads with array values work without exceptions
  • ✅ All existing tests continue to pass

Example Usage

// This now works without throwing exceptions
Http::attach('attachment', file_get_contents('photo.jpg'), 'photo.jpg')
    ->post('http://example.com/users', [
        'name' => 'Steve',
        'roles' => ['Network Administrator', 'Janitor'], // Array values
    ]);

Fixes #55732

joteejotee and others added 2 commits July 15, 2025 22:49
- Fixed parseMultipartBodyFormat to handle array values correctly
- Array values are now formatted as 'field[]' entries for multipart/form-data
- Maintains backward compatibility with existing name/contents format
- Added tests for array values and mixed file+array scenarios

Fixes laravel#55732
@taylorotwell taylorotwell merged commit 347cc4a into laravel:12.x Jul 18, 2025
60 checks passed
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.

Http Client mangles multiple-valued keys in parseMultipartBodyFormat
2 participants