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
Posted by Thomas Rothe (burnred) on 2011-03-11T08:43:06.000+0000
This Fix is the problem.
Posted by Bart McLeod (mcleod@spaceweb.nl) 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 (mcleod@spaceweb.nl) 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 (mcleod@spaceweb.nl) on 2011-10-12T22:10:01.000+0000
Added patches for the Rest client and the test.
Posted by Bart McLeod (mcleod@spaceweb.nl) on 2011-10-12T22:10:38.000+0000
Changed to patch.
Posted by Bart McLeod (mcleod@spaceweb.nl) on 2011-10-17T20:03:21.000+0000
Now in svn.
Posted by Bart McLeod (mcleod@spaceweb.nl) 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:
A request to this method could look like this: http://localhost/rest/…
And the response would be:
Posted by Bart McLeod (mcleod@spaceweb.nl) on 2011-11-08T22:48:21.000+0000
Recommend to not integrate in ZF 2