dongsu1539 2016-03-01 21:47
浏览 97
已采纳

如何强制204 rest API返回内容?

I know that 204 means that the server has understood the request but there is no content available. But I think is wrong prevent to the developer return a json message like this:
return json_encode(array("mex" => "no content"));

What I need is return also a json when the API return 204 in the header. I don't want use 200 in this case 'cause this means that there is a content returned.

Also the 404 is not appropriate 'cause this means the resource doesn't exits.
Anyidea to do this?
This is my code:

http_response_code(204);
return json_encode(array("mex" => "no content"));
  • 写回答

1条回答 默认 最新

  • dongzhanlu8890 2016-03-01 22:12
    关注

    You can't. The HTTP specification states that:

    The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

    Attempting to include content in a 204 response will cause many HTTP clients to malfunction. If you want to include a message with your response, you will need to use a response code other than 204.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?