A Contents Key Is required - Sending Arrays

443 Views Asked by At

I am having issues sending data over Laravel HTPP. I have an array of data and x number of files to be attached. i have managed to attach the file they are sending and being delivered to the endpoint correctly. But the actual data still give me the following error:

InvalidArgumentException
A 'contents' key is required

In this case its not because of attachments because attachment are fine.

public function step_5()
{
    $applications = request('item');
    $application_requirement = null;
    $data = array();
    $files = array();
    $response = '';
    foreach ($applications as $k => $application) {
        $application_requirement['application_id'] = $application['application_id'];
        $application_requirement['payment_requirement_id'] = $application['payment_requirement_id'];
        $application_requirement['payment_item_id'] = $application['payment_item_id'];
        //store files
        $files[] = $application['file'];
        //store data when looping
        $data[$k] = $application_requirement;
    }

    $response = Http::withHeaders(['Accept' => 'application/json']);
    foreach ($files as $k => $file) {
        $name = $file->getClientOriginalName();
        $response = $response->attach('file[' . $k . ']', fopen($file, 'r'), $name);
    }
    $json = $response->post('http://localhost:8000/api/applications/submit_documents',
        $data
    );
    $app = json_decode($json);
    dd($app);

}

and my blade is like:

@foreach($requirements as $requirement)
    <div id="" class="p-5">
        <div class="preview">
            <div class="flex flex-col sm:flex-row items-center">
                <label>{{$requirement->requirement}}
                    <input type="file" name="item[{{$requirement->id}}][file]"
                           class="form-control col-span-6"
                           aria-label="Document">
                </label>
            </div>

            <input type="hidden"
                   name="item[{{$requirement->id}}][payment_requirement_id]"
                   value="{{$requirement->id}}" class="form-control col-span-6">

            <input type="hidden" name="item[{{$requirement->id}}][application_id]"
                   value="{{$application->id}}" class="form-control col-span-6">

            <input type="hidden" name="item[{{$requirement->id}}][payment_item_id]"
                   value="{{$payment_item->id}}" class="form-control col-span-6">

        </div>

    </div>
@endforeach

If I die and dump the data it displays like whats in my attachment:

this is the data I want to post

0

There are 0 best solutions below