0
//These Images
    for($i = 0 ; $i < count($request['images']); $i++)
    {
        $response = $response->attach('images['.$i.']', file_get_contents($request['images'][$i]), 'image.png' );
    }
    $response = $response->post('...WEB_URL_HIDDEN.../api/merchant/createDeal', [
        'name' => 'name',
        'discount' => $request->discount,
        'type' => $request->type,
        'category' => $request->category,
        'actual_price' => $request->actual_price,
        'price' => $request->price,
        'after_discount' => $request->after_discount,
        'expiry' => $request->expiry,
        'description' => $request->description,

        // //These Arrays
        'branches' => $request['branches'],
        'tags' => $request['tags'],
    ]);
    dd($response->json());

This is my Code for sending Some images and Some Data to an external API. I am sending images in attach() function and sending arrays down there called 'branches' and 'tags'. Now the problem is this that When I submit an error comes as " A 'contents' key is required ". If I comment out attach() then it works well but images do not uploaded. And If a comment out 'branches' and 'tags' arrays then everything works find but arrays do not send to API. In a nutshell, the problem is the trade off between images and arrays.

  • 1
    Sorry, but that's a mess. Call your requests `$request` and responses `$response`, maybe? – Martin Zeitler Jun 28 '22 at 07:53
  • Does this answer your question? [Upload file using Guzzle 6 to API endpoint](https://stackoverflow.com/questions/38133244/upload-file-using-guzzle-6-to-api-endpoint) – Martin Zeitler Jun 28 '22 at 07:55
  • Sir in that link only one image upload is shown. I want to send a dynamic number of images coming from my html form. – AbdurRehman Jun 28 '22 at 08:55
  • By 'Sorry, but that's a mess. Call your requests $request and responses $response, maybe?'. What are you trying to suggest. I am kind of new in field. – AbdurRehman Jun 28 '22 at 08:59
  • use [concurrent request](https://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests) to send multiple files you are sending one by one (If it has a lot of images keep waiting for ages) – bhucho Jun 28 '22 at 13:54
  • write more about what type of request you want to send the content-type of the request how do you want to send it – bhucho Jun 28 '22 at 13:55

0 Answers0