Restler icon indicating copy to clipboard operation
Restler copied to clipboard

Content-Type shouldn't be sent with HTTP 204?

Open shaneneuerburg opened this issue 11 years ago • 1 comments

Another library, Siesta for iOS, expects an HTTP 204 to not include the Content-Type (or, I should say that if that is present, they try to parse the non-existent body).

The RFC states the following:

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body.

While it doesn't explicitly state that the header shouldn't be sent if there is no body, it only states that it should if the body exists.

I'd suggest moving the Content-Type generation logic after the status code assignment, with it wrapped in a conditional where $code != 204.

Thoughts?

shaneneuerburg avatar Nov 15 '15 07:11 shaneneuerburg

While we think about the long term solution for this problem, you can easily implement a quick fix

Compose class is responsible for final response, so you can create a custom Compose class by implementing iCompose interface and then set Defaults::$composeClass to the class name of your custom class. Here is an example

class CustomCompose implements iCompose
{
    public static $includeDebugInfo = true;
    public $restler;

    public function response($result)
    {
        if ($this->restler->responseCode == 204) {
            return '';
        }
        return $result;
    }

    public function message(RestException $exception)
    {
        $r = array(
            'error' => array(
                    'code' => $exception->getCode(),
                    'message' => $exception->getErrorMessage(),
                ) + $exception->getDetails()
        );
        if (!Scope::get('Restler')->getProductionMode() && self::$includeDebugInfo) {
            $r += array(
                'debug' => array(
                    'source' => $exception->getSource(),
                    'stages' => $exception->getStages(),
                )
            );
        }
        return $r;
    }
}

Arul- avatar Nov 29 '15 15:11 Arul-