Skip to content

Commit

Permalink
Add SetAcceptHeader middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans authored and jiannei committed May 31, 2022
1 parent 16b0021 commit 0b398ff
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Http/Middleware/SetAcceptHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Jiannei\Response\Laravel\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class SetAcceptHeader
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $type
*
* @return \Illuminate\Http\Response
*/
public function handle(Request $request, Closure $next, string $type = 'json')
{
Str::contains($request->header('Accept'), $contentType = "application/$type") or
$request->headers->set('Accept', $contentType);

return $next($request);
}
}

0 comments on commit 0b398ff

Please sign in to comment.