Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

fix(x): send JSON body on chunked media upload finalize

Paulo Castellano8f4d33cc11a4
diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.php
index 29cfd466f..e1f2667af 100644
--- a/app/Services/Social/XPublisher.php
+++ b/app/Services/Social/XPublisher.php
@@ -243,9 +243,10 @@ class XPublisher
$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 @@ class XPublisher
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 150716a5d..f2e7d2820 100644
--- a/tests/Feature/Services/Social/XPublisherTest.php
+++ b/tests/Feature/Services/Social/XPublisherTest.php
@@ -626,6 +626,72 @@ test('x publisher uploads video via chunked upload', function () {
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' => [