Search code examples
phprestler

How to not send a response body


I'm trying to send a header response back from our api with a http status code 201 Created and a Location:header.

No matter what I do I get a response body too, something that I don't want.

If I return an empty string (return "";), restler will put the string '""' in the response body. If I return null or do not return anything at all restler will put the string 'null' in the response body.

How do I tell Restler to not send anything but headers?


Solution

  • UPDATE :-

    With the latest release of Restler 3 RC4. Returning null sends empty body for the response

    This behaviour can be changed by setting

    Defaults::$emptyBodyForNullResponse = false;
    

    You can use @status comment to set the response code to 201

    and @header comment for setting the location header

    For older versions use the technique described below


    From your api method, set both status and location header using header function followed by die or exit

    header("HTTP/1.0 201 Created");
    header('Location: http://api.example.com/item/45');
    die();
    

    This is a very valid use case that demands better way of doing this, We will soon update this answer with those solutions

    Thanks for contributing to Restler :)