Image upload using Guzzle

abacusabacus Posts: 4Member

I'm struggling to upload images. I'm getting 500 internal server errors so I'm assuming I'm sending malformed requests.

This is all I've managed to come up with:

$itemResponse = $this->lightspeedClient->post('Item/' . $itemID . '/Image.json', 
      [
      'json' => [
        "description" => $itemID,
        "ordering" => 0
      ],
      'multipart' => [
        [  
          'name' => "image",
          'contents' => $contents,
        ]
      ] 
    ]);

Could anyone point me in the right direction? I know my snippet is a little vague. Could anyone post an example of uploading an image using guzzle?

1 comment

  • abacusabacus Posts: 4Member
    edited March 21

    I eventually solved this. In case anyone else is curious, multipart cannot be used with json using Guzzle.

    http://docs.guzzlephp.org/en/stable/request-options.html#multipart

    Something like this is what worked for me:

    $itemResponse = $this->lightspeedClient->post('Item/' . $itemID . '/Image.json', 
            [
                'multipart' => [
                    [
                        'name' => 'image',
                        'filename' => $filename,
                        'Content-Type' => 'image/jpeg',
                        'contents' => $contents,
                    ],
                    [
                        'name' => 'data',
                        'contents' => json_encode(
                            [
                                'description' => $filename,
                                'ordering' => $ordering,
                            ]
                        ),
                    ]
                ]
            ]);
    
Sign In or Register to comment.