GodziLaravel's avatar

Why destroy() does not returns any response?

Hello

I developed the method bellow to delete , everything works well except that I don't get the confirmation message ['message' => 'Line deleted successfully'], but I get the response code : 204.

Any idea?

public function destroy($id): JsonResponse
{
    $lineData = $this->validateAndRetrieveLine($id);

    if ($lineData['status'] !== null) {
        return response()->json(['message' => $lineData['message']], $lineData['status']);
    }

    $line = $lineData['line'];

(...)

    // Soft delete the line
    $line->delete();

    // Return a JSON response indicating success
    return response()->json(['message' => 'Line deleted successfully'], 204);
}
0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

204 is the No Content status code; hence no message!

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

https://www.rfc-editor.org/rfc/rfc2616#section-10.2.5

Use a different status code, e.g. 200 would seem appropriate in this situation

Please or to participate in this conversation.