-2

I'm trying to send a set of form inputs from a controller into an API endpoint but when I check the variables in the API, it all shows null value.. what is wrong with my code?

CulinaryController.php

    public function update(Request $request, Culinary $culinary)
    {
        $client = new Client();
        $token = Session('token');

        try {
            $multipart = [];

            foreach ($request->except('cover_photo') as $key => $value) {
                $multipart[] = [
                    'name' => $key,
                    'contents' => $value,
                ];
            }
            
            if ($request->hasFile('cover_photo')) {
                $coverPhoto = $request->file('cover_photo');
                $multipart[] = [
                    'name' => 'cover_photo',
                    'contents' => fopen($coverPhoto->getRealPath(), 'r'),
                    'filename' => $coverPhoto->getClientOriginalName(),
                ];
            }
            dd($multipart);
            
            $url = 'http://localhost/api/v1/culinary/'.$culinary->id;
            $response = $client->put($url, [
                'multipart' => $multipart,
                'headers' => [
                    'Authorization' => 'Bearer '.$token,
                    'Accept' => 'application/json',
                ]
            ]);
        } catch (Exception $e) {
            return redirect()->back()->withErrors(['errors' => $e->getMessage()])->withInput();
        }
    }

api/v1/CulinaryController.php

    public function update(Request $request, Culinary $culinary)
    {
        return response()->json([
            'status' => 'test',
            'request' => $request->all(),
            'foo' => request('title'),
            'file' => $request->file('cover_photo'),
            'culinary' => $culinary,
        ]);
    }

routes/api.php

    Route::apiResource('culinary', CulinaryController::class);

when I uncomment the dd($multipart) in the controller, it contains 7 sets of input below so I'm sure the form wasn't empty when submitted.

array:9 [▼ // app\Http\Controllers\AdminCulinaryController.php:198
  0 => array:2 [▼
    "name" => "_token"
    "contents" => "TwpS0e7OJH1jhhR1dr8RBpCiYKW4vhVyuk3CRZWC"
  ]
  1 => array:2 [▼
    "name" => "_method"
    "contents" => "PUT"
  ]
  2 => array:2 [▼
    "name" => "title"
    "contents" => "Et in quia ut et illo harum."
  ]
  3 => array:2 [▼
    "name" => "old_cover_photo"
    "contents" => "https://via.placeholder.com/640x480.png/0022bb?text=distinctio"
  ]
  4 => array:2 [▼
    "name" => "country_id"
    "contents" => "188"
  ]
  5 => array:2 [▼
    "name" => "category_name"
    "contents" => "vegetarian"
  ]
  6 => array:2 [▼
    "name" => "prologue"
    "contents" => "Adipisci iure nihil sit voluptatem rerum voluptatibus. Qui aperiam et magnam corrupti quia eius reprehenderit. Sunt non aut dolores. Dolorem nesciunt atque ipsa
 ▶
"
  ]
  7 => array:2 [▼
    "name" => "body"
    "contents" => "Autem iure qui magni excepturi hic. Laudantium adipisci saepe odio quod cupiditate. Blanditiis occaecati sint at sint incidunt optio illum nihil."
  ]
  8 => array:2 [▼
    "name" => "opening_hours"
    "contents" => "Qui et magnam labore a ea corrupti."
  ]
]
9
  • What Client are you making your request with? Commented Aug 14, 2024 at 10:41
  • @OluwafemiSule I'm using guzzlehttp Commented Aug 14, 2024 at 10:57
  • I think your answer lies here. PHP supports multipart/form-data only for POST requests and not PUT. Commented Aug 14, 2024 at 12:09
  • The issue is fixed in PHP 8.4. You'll likely have to update your PHP version to resolve it. Commented Aug 14, 2024 at 12:15
  • if I'm not mistaken, PHP 8.4 isn't released yet, right? Commented Aug 15, 2024 at 7:28

2 Answers 2

0
$response = $client->put($url, [
    'multipart' => $multipart,
    'headers' => [
        'Authorization' => 'Bearer '.$token,
        'Accept' => 'application/json',
        'Content-Type' => 'multipart/form-data',
    ]
   ]);

May be update content-type and try

Sign up to request clarification or add additional context in comments.

Comments

-2

I studied your question . you can use a below package for a solution your problem :

composer require alireaza/php-form-data

In php we have multipart/form-data in POST method request but you have use PUT method.i have a suggest for you. please test $request->all() at the beginning of your function (update) and check type of 'cover-photo' parameter. if you recieve array type its ok but you should use a package for config multipart/form-data on your PUT Request.

good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.