Response with status 204, remove content-length header


#1

I need to remove the Content-Length header on the response object when status code === 204

I’ve tried the following
$response = $response->withoutHeader(‘Content-Length’);
return $response->withStatus(204);

Content-Length header is always 20, no matter what.

Anyone can help?


#2

Maybe you need to set addContentLengthHeader to false instead:

$config = [
    'settings' => [
        'addContentLengthHeader' => false,
    ]
];
$app = new Slim\App($config);

#3

I’ve just created a dummy project, using the Skelleton app which as to false by default

composer create-project slim/slim-skeleton testEmpty

I’ve added a route

$app->get('/empty', function ($request, $response, $args) {
    return $response->withStatus(204);
});

And the Content-Length header is present with a 0 value


#4

This is probably getting set by your web server.


#5

Yes. This is what I just found out. Removing the Content-Encoding (mod deflate with Apache) makes the Content-Length value as 0 instead or 20 which is already good.

Not a Slim issue.