Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheoR's avatar
Level 14

Laravel HTTP Client JSON with FORCE_OBJECT set

I have a api I am trying to send to. It requires empty arrays to have curly brackets "{}" instead of regular brackets "[]".

Here is my code that i am using to post to the api:

       $sendReport = Http::withToken($authResponse)
            ->contentType('application/json')
            ->post($crashExportServer . $uploadPoint, $data)
            ->json();

$data = an array. This works, but I have the issue with the empty arrays within $data. I get an error back in regards to them. I can use json_encode and pass the parameter of "JSON_FORCE_OBJECT" and then it looks right to me. However, I'm stumped on how to do then when using the http client. I tried replacing the $data array with a string of the encoded JSON string, but that fails:

       $jsonData = json_encode($data, JSON_FORCE_OBJECT);
       $sendReport = Http::withToken($authResponse)
            ->contentType('application/json')
            ->post($crashExportServer . $uploadPoint, $jsonData)
            ->body();

Here, $jsonData is the output of, but the api doesn't like that at all. I also tried:

       $jsonData = json_encode($data, JSON_FORCE_OBJECT)
       $sendReport = Http::withToken($authResponse)
            ->contentType('application/json')
            ->post($crashExportServer . $uploadPoint, ['body' => $jsonData])
            ->body();

But the api doesn't seem to like that either.

Any help would be greatly appreciated! Thanks!

0 likes
8 replies
Sinnbeck's avatar

Did you try

$jsonData = json_decode(json_encode($data, JSON_FORCE_OBJECT)); 
TheoR's avatar
Level 14

@Sinnbeck Thanks for the reply. I'm able to create the Json String correction with json_encode. The problem is, on the "Http" request, I need to tell Guzzle to json_encode with "JSON_FORCE_OBJECT" but I don't see how to do this. Or how to pass a raw json string that I decode before hand.

Sinnbeck's avatar

@TheoR did you try my code or? Notice I make it back into a php array, but it should now use objects as empty arrays

TheoR's avatar
Level 14

@Sinnbeck I did, but it was pretty much the same as the standard json_encode with JSON_FORCE_OBJECT. My problem was more that I wanted to pass the JSON_FORCE_OBJECT parameter into the "post" command of the guzzle (Http) send.

TheoR's avatar
Level 14

Update, I figured out how to get past the issue by using "withBody":

$sendReport = Http::withToken($authResponse)
    ->withBody($jsonData, 'application/json')
    ->post($crashExportServer . $uploadPoint)
    ->json();

Now, I still have issues with the api standards (it's an .Net Api, go figure!). But at least I can manipulate the json now. :)

SyedGhazanfar's avatar

You sholud use get_object_vars(json_decode($sendReport)), this will give you as PHP Object

TheoR's avatar
Level 14

@SyedGhazanfar I'm not trying to decode the json, I'm tying to encode it, but the .Net specs of the api I'm sending to didn't like the empty objects as [], it wants {}.

Luckily, I did find a work around. And I've now found more issues that .Net is expecting in the json data that I'm having to change up.

Please or to participate in this conversation.