From e4c0dd5669ad87638c1fe08c4e8347e4197ff19a Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 27 May 2026 14:29:07 +0100 Subject: [PATCH] Preserve empty HTTP attach contents --- src/Illuminate/Http/Client/PendingRequest.php | 11 +++++-- tests/Http/HttpClientTest.php | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index f850495c9873..6ee9587fac4e 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -351,12 +351,17 @@ public function attach($name, $contents = '', $filename = null, array $headers = $this->asMultipart(); - $this->pendingFiles[] = array_filter([ + $file = [ 'name' => $name, 'contents' => $contents, 'headers' => $headers, - 'filename' => $filename, - ]); + ]; + + if ($filename !== null) { + $file['filename'] = $filename; + } + + $this->pendingFiles[] = $file; return $this; } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 6e5040d77a43..0d0e339bfb31 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -795,6 +795,35 @@ public function testFilesCanBeAttached() }); } + public function testAttachPreservesEmptyContents() + { + $this->factory->fake(); + + $this->factory->attach('file')->post('http://foo.com/file'); + + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/file' + && $request->isMultipart() + && $request[0]['name'] === 'file' + && array_key_exists('contents', $request[0]) + && $request[0]['contents'] === ''; + }); + } + + public function testAttachPreservesFalseyStringContentsAndName() + { + $this->factory->fake(); + + $this->factory->attach('0', '0')->post('http://foo.com/file'); + + $this->factory->assertSent(function (Request $request) { + return $request->url() === 'http://foo.com/file' + && $request->isMultipart() + && $request[0]['name'] === '0' + && $request[0]['contents'] === '0'; + }); + } + public function testCanSendMultipartDataWithSimplifiedParameters() { $this->factory->fake();