ZF-10664: cannot send file using Zend_Rest_Client

Description

Ex:

$client = new Zend_Rest_Client($someDomain);

$httpClient = new Zend_Http_Client(); $httpClient->setFileUpload('/var/www/test/public/images/cat.jpg', 'picture');

$client->setHttpClient($httpClient);

$response = $client->restPost('/somecontroller/2');

/** this code is not working because when you call restPost will call _prepareRest() in code below

final public function restPost($path, $data = null)
{
    $this->_prepareRest($path);
    return $this->_performPost('POST', $data);
}

and _prepareRest() will call resetParameters()

in line

self::getHttpClient()->resetParameters()->setUri($this->_uri);

and this will erase all uploadedFile from variabile $files in method resetParameters($clearAll = false)

    // Reset parameter data
    $this->paramsGet     = array();
    $this->paramsPost    = array();
    $this->files         = array();
    $this->raw_post_data = null;

So the files cannot be sent by restPost()

Comments

This Fix is the problem.

I am currently fixing the problem that Zend_Rest_Client can't send PUT data. I think that after that fix is committed, you should be able to upload a file using restPut. I will link the issue.

I have created a solution wiht an extra function setNoReset($bool = true) that you can call prior to calling restPost(). No reset of the Zend_Http_Client will take place if you set the noReset flag. Would this solution be acceptable? I am working on the unit test (to simplify it). After I'm finished, I'll upload the patches. I would like to know if adding this extra call is acceptable.

Added patches for the Rest client and the test.

Changed to patch.

Now in svn.

Based on your [the reporter] (private) feedback, that I requested, I agree that this is only a workaround. However, if you are looking to post files with Zend_Rest_Client you are doing it wrong. Zend_Rest_Client is a high level client, that is intended for use with Zend_Rest_Server, as it is designed at present. Future implementations will likely be able to talk to different types of servers, but as it is now, Zend_Rest_Client is only a convenient way to call class methods on the server as rest methods. Therefore, if you want to post a file, you should create a method on the server that can take a filename and file contents as arguments. An example server would look like this:


<?php
class TestClass {
    public function saveFile($contents, $filename)
    {
        $path = '/tmp/' . $filename;
        file_put_contents ($path, $contents);
        return file_get_contents($path);
    }
}

class RestController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $server = new Zend_Rest_Server();
        $server->setClass('TestClass');
        $server->handle($_REQUEST);
        exit();
    }
}

A request to this method could look like this: http://localhost/rest/…

And the response would be:


bogussuccess

Recommend to not integrate in ZF 2