How to send image via form with cakephp 4 to backend via REST API?

I’m trying to send an image to a registration service made in Node.js.

I have a form made like this:

 <?= $this->Form->create($product, ['enctype' => 'multipart/form-data']); ?>
            <div class="card-body">
                <div class="row mt-3">
                    <div class="col-6">
                        <input type="file" name="file"/>
                    </div>
                </div>
            </div>
            <div class="card-footer">
                <button type="submit" class="btn btn-info">Save</button>
        </div>
    </form>
 </div>

In the controller I get the image like this:

$image = $this->request->getData("file");
debug($image);
$response = $http->post('http://localhost:8889/api/.../new/',
    [
      'file' => $image, 
    ]
);

The result is this:

object(Laminas\Diactoros\UploadedFile) id:0 {
private clientFilename => 'Captura de Tela 2023-01-02 às 18.22.54.png'
private clientMediaType => 'image/png'
private error => (int) 0
private file => '/Applications/MAMP/tmp/php/phpLuDfvm'
private moved => false
private size => (int) 1024251
private stream => null
}

My problem at the moment is that I don’t receive the image in the backend. The information is received as undefined

How can I correctly send cakephp 4 image file to backend?

what do you mean by The information is received as undefined. What is your backend here? CakePHP or the NodeJS app?

you literally have the UploadedFile object containing all the important information about your file present inside your controller.

If you wanna move/create that file in a specific folder you have to

$image->moveTo(WWW_ROOT . 'images');

and it will be saved to your webroot/images folder

My backend is on Node.js. The image would be saved in a folder created in the project in Node.js. However, this does not happen. I’m having trouble uploading the image correctly. I understand your approach to saving to webroot, but I wanted to handle this via the web service.

I don’t really understand what the job of CakePHP here is.
Why post a form with a image to CakePHP if all its job is just to send it away again with another POST request. Why not send the image directly to your NodeJS app?

Anyway: What you probably need is the resource object behind that UploadedFile object.
So try

$response = $http->post('http://localhost:8889/api/.../new/',
    [
      'file' => $image->getStream()->detach(), 
    ]
);

As you can see in Http Client - 4.x the HTTP Client requires something of type resource to send as a file because that is what fopen() will return.

And $image->getStream()->detach() will return the underlying resource of your uploaded file since your $image is of type \Laminas\Diactoros\UploadedFile

My frontend is made using cakephp 4. I made a product registration form. In this case, just insert an image in the field and consume an API. The problem is that I can’t send the image to a service made in Node.js

I added your suggestion, but to no avail!

'file' => $image->getStream()->detach(),