Search code examples
phpjsonrestler

Restler, not serving data with force download set to true?


I have a weird problem here, When using the restler api, it seems as if the headers Content Type resets even if I forcefully set it to something different... It seems as if $r = new Restler(); $r->setSupportedFormats('JsonFormat'); Always resets it to "Content-Type: application/json; charset=utf-8"

So, any way I can manually override the default mime type to what I want it to be? Other then that nothing seems wrong, here's how it looks when you request a file download: Cache-Control: no-cache, must-revalidate Connection: Keep-Alive Content-Disposition: attachment; filename="test.pdf" Content-Encoding: gzip Content-Language: en Content-Length: 25 Content-Type: application/json; charset=utf-8

And this is what I set in the phpcode:

header("Content-type: application/pdf"); header("Content-Length: ". $data["Size"]); header('Content-Disposition: attachment; filename="'. $data["Name"].'"');

Any help to force it as the content type I please? (jftr, pdf is an example, there are many more possible types)


Solution

  • Restler sets the headers after we return the result from our api method

    If we want to override this default behaviour, we need to stop executing further, just add die/exit as shown below on your api method

    header("Content-type: application/pdf"); 
    header("Content-Length: ". $data["Size"]); 
    header('Content-Disposition: attachment; filename="'. $data["Name"].'"');
    die();
    

    HTH