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

Waweru's avatar

JSON structure for form-data in Laravel API

Hi. I have a Laravel API that receives an file and additional body data from a JavaScript client.

How should the JSON data format look like when I send the data?

0 likes
6 replies
manojo123's avatar

Please provide more information to your problem.

jlrdw's avatar

Try sending some as test, as long as it's properly formatted Json. The trick will be decoding and looping over it properly once received.

Waweru's avatar

I have tried lots of tests but none works because I don't understand what the Laravel API wants from the client side.

jlrdw's avatar

Well the docs show a normal array prior to being json: From docs.

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }

And docs say:

Every resource class defines a toArray method which returns the array of attributes that should be converted to JSON when sending the response.

So sending or receiving you have to have well formed array to get well formed Json.

You receive the data through an api route, then do whatever you need to do with it.

Find this line Retrieving JSON Input Values

Here: https://laravel.com/docs/5.7/requests

Waweru's avatar

Thanks everyone. I found the solution by appending FormData of the file data. Sorry I was not able to communicate the issue well. Thanks again.

Please or to participate in this conversation.