ZF-10664: cannot send file using Zend_Rest_Client

Issue Type: Patch Created: 2010-11-09T13:07:18.000+0000 Last Updated: 2011-11-08T22:49:38.000+0000 Status: Closed Fix version(s): Reporter: Sergiu Neamt (nnsese) Assignee: Bart McLeod ( Tags: - Zend_Rest_Client

Related issues: - ZF-1853

Attachments: - Client.php.patch



$client = new Zend_Rest_Client($someDomain);

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


$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)
    return $this->_performPost('POST', $data);

and _prepareRest() will call resetParameters()

in line


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()


Posted by Thomas Rothe (burnred) on 2011-03-11T08:43:06.000+0000

This Fix is the problem.

Posted by Bart McLeod ( on 2011-07-21T09:31:20.000+0000

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.

Posted by Bart McLeod ( on 2011-10-11T21:27:56.000+0000

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.

Posted by Bart McLeod ( on 2011-10-12T22:10:01.000+0000

Added patches for the Rest client and the test.

Posted by Bart McLeod ( on 2011-10-12T22:10:38.000+0000

Changed to patch.

Posted by Bart McLeod ( on 2011-10-17T20:03:21.000+0000

Now in svn.

Posted by Bart McLeod ( on 2011-11-08T22:46:37.000+0000

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:

<pre class="highlight">
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();

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

And the response would be:

<pre class="highlight">

Posted by Bart McLeod ( on 2011-11-08T22:48:21.000+0000

Recommend to not integrate in ZF 2

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.