Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel how to response only 204 code status with no body message

Tags:

php

laravel

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

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?

like image 842
Stefano Maglione Avatar asked Apr 23 '18 01:04

Stefano Maglione


People also ask

How can I return 204 without content?

By default, 204 (No Content) the response is cacheable. If caching needs to be overridden then the response must include cache respective cache headers. For example, you may want to return status 204 (No Content) in UPDATE operations where request payload is large enough not to transport back and forth.

Can 204 response have body?

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

How do I respond to a status code in laravel?

You can use http_response_code() to set HTTP response code. If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.

What does 204 no content mean?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page. This might be used, for example, when implementing "save and continue editing" functionality for a wiki site.


3 Answers

You can also do return response()->noContent();

like image 88
Mark Avatar answered Sep 23 '22 12:09

Mark


Try return Response::make("", 204);

Update: Mark's answer is more up to date:

return response()->noContent()
like image 36
DiegoSalazar Avatar answered Sep 26 '22 12:09

DiegoSalazar


You may return 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.

like image 39
brad Avatar answered Sep 24 '22 12:09

brad