diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.php index 29cfd466..e1f2667a 100644 --- a/app/Services/Social/XPublisher.php +++ b/app/Services/Social/XPublisher.php @@ -243,9 +243,10 @@ private function chunkedUpload(string $tempFile, int $totalBytes, string $mimeTy $initPayload['media_category'] = $mediaCategory; } - // INIT + // INIT — X requires application/json (MediaUploadConfigRequest). $initResponse = $this->socialHttp()->withToken($this->accessToken) ->timeout(60) + ->asJson() ->post("{$this->baseUrl}/media/upload/initialize", $initPayload); if ($initResponse->failed()) { @@ -307,9 +308,12 @@ private function chunkedUpload(string $tempFile, int $totalBytes, string $mimeTy fclose($handle); } - // FINALIZE - Use the new v2 endpoint + // FINALIZE — runtime rejects empty / non-JSON bodies with + // "Request body must be a JSON object." Send `{}` explicitly + // (empty array encodes as `[]`, which is not an object). $finalizeResponse = $this->socialHttp()->withToken($this->accessToken) ->timeout(60) + ->withBody('{}', 'application/json') ->post("{$this->baseUrl}/media/upload/{$mediaId}/finalize"); if ($finalizeResponse->failed()) { diff --git a/tests/Feature/Services/Social/XPublisherTest.php b/tests/Feature/Services/Social/XPublisherTest.php index 150716a5..f2e7d282 100644 --- a/tests/Feature/Services/Social/XPublisherTest.php +++ b/tests/Feature/Services/Social/XPublisherTest.php @@ -626,6 +626,72 @@ Http::assertSent(fn ($request) => str_contains($request->url(), '/2/tweets')); }); +test('x publisher sends JSON object bodies for chunked upload initialize and finalize', function () { + $this->post->update([ + 'media' => [ + [ + 'id' => 'test-media-video', + 'path' => 'media/2026-01/test-video.mp4', + 'url' => 'https://example.com/media/2026-01/test-video.mp4', + 'mime_type' => 'video/mp4', + 'original_filename' => 'test-video.mp4', + ], + ], + ]); + + Http::fake(function ($request) { + $url = $request->url(); + + if (str_contains($url, '/2/media/upload/initialize')) { + return Http::response(['data' => ['id' => 'media_id_json']], 200); + } + + if (str_contains($url, '/append')) { + return Http::response(null, 204); + } + + if (str_contains($url, '/finalize')) { + return Http::response(['data' => ['id' => 'media_id_json']], 200); + } + + if (str_contains($url, '/2/media/')) { + return Http::response(['processing_info' => ['state' => 'succeeded']], 200); + } + + if (str_contains($url, '/2/tweets')) { + return Http::response(['data' => ['id' => '1111222233334444', 'text' => 'Hello from X!']], 200); + } + + return Http::response('fake-video-content', 200); + }); + + $this->publisher->publish($this->postPlatform); + + Http::assertSent(function ($request) { + if (! str_contains($request->url(), '/2/media/upload/initialize')) { + return false; + } + + $contentType = $request->header('Content-Type')[0] ?? ''; + + return str_contains($contentType, 'application/json') + && data_get($request->data(), 'media_type') === 'video/mp4' + && data_get($request->data(), 'media_category') === 'tweet_video'; + }); + + Http::assertSent(function ($request) { + if (! str_contains($request->url(), '/finalize')) { + return false; + } + + $contentType = $request->header('Content-Type')[0] ?? ''; + + // Empty PHP array encodes as `[]`; X requires a JSON *object* (`{}`). + return str_contains($contentType, 'application/json') + && $request->body() === '{}'; + }); +}); + test('x publisher uploads a large video in sequential 1MB segments', function () { $this->post->update([ 'media' => [