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

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Laravel Custom Response Handling for Empty Body Messages and 204 Status Codes Introduction In some cases, you might need to return an empty response containing no content but still require a specific HTTP response code, such as 204 (No Content). This is often useful when handling API requests that don't modify existing data or where the client doesn't expect any payload. With Laravel, it is simple to achieve this with custom response handling. The given example code `return response(null, 204);` returns an empty body and a 204 status code. However, a common issue arises when parsing the response in other applications, such as a Ruby script using JSON.parse(res.body). In this case, unexpected data is returned:
{"data"=>[]}
The "data" key-value pair and its associated empty array is still present in the response even though there's no actual content within it. To address this issue, you can customize your Laravel application to send only the desired status code without an accompanying body message. Solution: Using Laravel Middleware To achieve a 204 status code with an empty response body, we will utilize middleware in our Laravel application. By adding the necessary logic within this layer of the application stack, you can control and customize your responses before they are sent to the client. 1. Create a custom middleware for handling the desired response: Create a new file named `NoContentResponseMiddleware.php` in your Laravel project's `app/Http/Middleware` directory. This will contain our custom middleware logic for sending empty responses with specific status codes. 2. Implement appropriate functionality within the middleware class: Include the `Handle` trait and define a `handle` method, as shown below. This method will return the response once it's executed for each request passing through the middleware.
namespace App\Http\Middleware;

use Closure;

class NoContentResponseMiddleware
{
    use \Illuminate\Routing\Middleware\HandleRequests, \Illuminate\Routing\Middleware\RedirectIfAuthenticated;

    public function handle($request, Closure $next)
    {
        // Your custom logic if needed (e.g., check for specific conditions)

        return response()
            ->setContent(null)
            ->setStatusCode(204);
    }
}
Your custom middleware can also include additional logic, such as checking specific conditions before sending the empty response. This allows you to handle different cases based on request parameters or other factors. 3. Register and apply the middleware: Add your new middleware to the list of registered middlewares within Laravel's `App\Providers\RouteServiceProvider` file, as follows:
namespace App\Providers;

use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends \Illuminate\Routing\RouteServiceProvider
{
    /**
     * The path to the "home" route.
     *
     * @var string
     */
    const HOME = '/';

    /**
     * Define your routes method.
     *
     * Listen for when an API request is made and apply custom middleware accordingly.
     */
    public function defineRoutes($router)
    {
        ...

        // Register your new middleware for specific routes or globally
        $this->app->bind(\Illuminate\Contracts\Foundation\Application::class, function () {
            return new \Illuminate\Foundation\Application(realpath(__DIR__ . '/../..'));
        });

        Route::middleware([
            ...
            App\Http\Middleware\NoContentResponseMiddleware::class,
        ])->group(function ($api) {
            // Custom route definitions...
        });
    }
}
4. Test your new response handling: Create a simple controller or API endpoint that utilizes the custom middleware to send an empty body message with a 204 status code. Make a request to this endpoint from your Ruby script to verify whether the unexpected data has disappeared from the JSON parsed response. Conclusion By combining Laravel's well-designed architecture and custom middleware, you can easily handle specific responses such as empty bodies with particular HTTP status codes. This allows you to provide clearer and more efficient responses for your API clients without complicating the server-side logic of your application. Remember to keep your code clean and organized by using natural backlinks to relevant resources on https://laravelcompany.com, where applicable.