POSTing image to Cloudflare Images using Guzzle

Hi There. I recently ran in to trouble uploading my image to the image service using guzzle. Receiving error message “ERROR 5400: Bad request: Error parsing form fields\in\n”. Below I have included a snippet that worked for me.



    private function upload($request){
        $endpoint = 'https://api.cloudflare.com/client/v4/accounts/yourAccountId/images/v1';
        $client = new \GuzzleHttp\Client();

        $response = $client->request('POST', $endpoint, [
            "headers" => [
                "Authorization" => 'Bearer ' . 'yourApiToken',
            ],
            'multipart' => [
                [
                    'name' => 'file',
                    'filename' => '@./' . $request->name,
                    'contents' =>  file_get_contents($request->image, 'r')
                ]
            ]
        ]);

        return $response->getStatusCode();
    }

This topic was automatically closed after 15 days. New replies are no longer allowed.