FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 704 forks source link

Additional LF LF when returning BinaryFileResponse #1971

Open litepl opened 5 years ago

litepl commented 5 years ago

Hi,

I have a strange problem. My FOSRestBundle API works great until I want do send BinaryFileResponse directly from FOSRestController based class. It adds double LF in the beginning (but size is not changed !). When I do it from "normal/common" controller, everything works.

Example code:

class APIArdokDocumentsController extends FOSRestController
{
...
// /api/v1/documents/4/content

public function getDocumentContentAction($id)
{
        ... // code to get $filename from $id
        $response = new BinaryFileResponse($filename);

        $response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
        $response->headers->set('Pragma', 'no-cache');
        $response->headers->set('Expires', '0');

        $mtg = new FileinfoMimeTypeGuesser;

        if($mtg->isSupported())
        {
            $response->headers->set('Content-Type', $mtg->guess($filename));
        }
        else
        {
            $response->headers->set('Content-Type', 'text/plain');
        }

        $response->setContentDisposition(
            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
            basename($filename)
        );

        return $response;
}
}

Exactly the same code called from non-api controller works...

Where I have made a mistake ?

Regards,

Lukasz

litepl commented 5 years ago

For now, I've created a workaround with view redirect to the other controller with the same code and it works.

I cannot find a place, where FosRestController returns those LFs, but the strange thing I noticed is when I used curl without -L to follow redirect, there where also double LFs returned.

Regards,

Lukasz

xabbuh commented 5 years ago

If you could create a small example application that allows to reproduce your issue, we could probably try to debug the issue.