ZF-6306: Zend_Rest_Client: Content-type for POST requests (JSON support)

Description


    /**
     * Perform a POST or PUT
     *
     * Performs a POST or PUT request. Any data provided is set in the HTTP
     * client. String data is pushed in as raw POST data; array or object data
     * is pushed in as POST parameters.
     *
     * @param mixed $method
     * @param mixed $data
     * @param string $enctype
     * @return Zend_Http_Response
     */
    protected function _performPost($method, $data = null, $enctype = null)
    {
        $client = self::getHttpClient();
        if (is_string($data)) {
            $client->setRawData($data, $enctype);
        } elseif (is_array($data) || is_object($data)) {
            $client->setParameterPost((array) $data);
        }
        return $client->request($method);
    }

    /**
     * Performs an HTTP POST request to $path.
     *
     * @param string $path
     * @param mixed $data Raw data to send
     * @param string $enctype
     * @return Zend_Http_Response
     */
    final public function restPost($path, $data = null, $enctype = null)
    {
        $this->_prepareRest($path);
        return $this->_performPost('POST', $data, $enctype);
    }

Comments

This is a quite big issue, need to implement a REST client for one of my clients, and they require a specific enctype to be set.

However, the above issue blocks me from implementing it with Zend_Rest_Client as the setRawData function call now clears my previously set enctype through self::getHttpClient()->setEncType('MY_ENCTYPE')

Bart, you are right. We just fought like maniacs here trying to figure out why it wouldn't work, and patching Zend_Rest_Client as patched above worked first shot. Thanks but please fix in the core framework in 1.11.12.

Wouldn't it be better to change Zend_Http_Client's setRawData() ?

We don't need to overwrite the EncType there if it is NULL!

e.g.

/**
     * Set the raw (already encoded) POST data.
     *
     * This function is here for two reasons:
     * 1. For advanced user who would like to set their own data, already encoded
     * 2. For backwards compatibilty: If someone uses the old post($data) method.
     *    this method will be used to set the encoded data.
     *
     * $data can also be stream (such as file) from which the data will be read.
     *
     * @param string|resource $data
     * @param string $enctype
     * @return Zend_Http_Client
     */
    public function setRawData($data, $enctype = null)
    {
        $this->raw_post_data = $data;
        if (!is_null($enctype)) {
            $this->setEncType($enctype);
        }
        if (is_resource($data)) {
            // We've got stream data
            $stat = @fstat($data);
            if($stat) {
                $this->setHeaders(self::CONTENT_LENGTH, $stat['size']);
            }
        }
        return $this;
    }