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.

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助