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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器