1

I try to send the file via GuzzleHTTP from my application to external API, I make it like this:

public function storeImagesInAmazon(Request $request) {

    $uploadFilePath = 'some/endpoint';
    $file = $request->file('file');

    $client = new Client();
    $response = $client->request('POST', $uploadFilePath, [
        'headers' => [
            'Accept'                => 'application/json',
            'Content-Type'          => 'multipart/form-data',
        ],
        'multipart' => [
            [
                'name'     => 'file',
                'contents' => $file
            ]
        ]
    ]);

    $result = json_decode($response->getBody(), true);

    return [
        'hashedID' => $result['hashedID']
    ];
}

The error I get is:

Server error: POST some/endpoint resulted in a 500 Internal Server Error response:\n Errorerror while processing file: Failed to process file: part was null

I tested it via Postman, adding key = 'file', value = 'some_file.pdf' in Body form-data, I am sure file is correct, I mean it isn't damaged, I tried to post different files large one, a small one, pdf or jpg/png.

But I still have this issue and I can't find a solution for this.

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
jakmen
  • 93
  • 10
  • Remove the `Content-Type` header. For a multipart request, this also needs to contain a proper _boundary_ value - which is completely missing from the one you are setting. By using the `multipart` key, guzzle should know that it has to set this on its own already. – CBroe Mar 09 '20 at 12:32
  • @CBroe I removed Content-Type header but then I got error : Not a multipart request, assuming part is null. :( I have tried solution from this topic: https://stackoverflow.com/a/40504054/10925079 But nothing has changed – jakmen Mar 09 '20 at 13:20
  • What does `$file = $request->file('file')` do? – CBroe Mar 09 '20 at 14:57
  • @CBroe In this way I just get file from request. I also tried pass file to 'contents' by 'contents' => fopen($file->getRealPath(), 'r') or 'contents' => file_get_contents($file) – jakmen Mar 09 '20 at 15:13

1 Answers1

0

I found this solution Guzzle form_params not accepting array

what I'm trying to say is, you need $option as your next param like in that post $response = $client->post('api', $options);

and that $option is where your headers or multipart or other options goes as per documentation. i already tried using $options and its worked in my case.