Laravel 如何仅响应 204 代码状态而没有正文消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49972284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel how to response only 204 code status with no body message
提问by Stefano Maglione
I am using
我在用
return response(null,204);
because I would like to return an empty body message, but the problem is that when I parse the response with a ruby code
因为我想返回一个空的正文消息,但问题是当我用 ruby 代码解析响应时
JSON.parse(res.body)
I get some body message:
我收到一些正文消息:
{"data"=>[]}
so how can I avoid to return this "data" and instead return only the status code?
那么我怎样才能避免返回这个“数据”而只返回状态代码呢?
采纳答案by DiegoSalazar
Try return Response::make("", 204);
尝试 return Response::make("", 204);
回答by Mark
You can also do return response()->noContent();
你也可以这样做 return response()->noContent();
回答by brad
You may return null:
您可能会返回 null:
return response(null, 204);
Edit: I didn't read the question before answering and I notice now that you are already doing this. I recommend you just do an empty / null check on the body in your Ruby code if possible.
编辑:我在回答之前没有阅读这个问题,现在我注意到你已经在这样做了。如果可能,我建议您只对 Ruby 代码中的主体进行空/空检查。