From 0b398ff78cb9667c9766f7fb7d3da112e70f64eb Mon Sep 17 00:00:00 2001 From: yaozm Date: Tue, 31 May 2022 20:03:28 +0800 Subject: [PATCH] Add `SetAcceptHeader` middleware --- src/Http/Middleware/SetAcceptHeader.php | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Http/Middleware/SetAcceptHeader.php diff --git a/src/Http/Middleware/SetAcceptHeader.php b/src/Http/Middleware/SetAcceptHeader.php new file mode 100644 index 0000000..857b292 --- /dev/null +++ b/src/Http/Middleware/SetAcceptHeader.php @@ -0,0 +1,36 @@ + + * + * 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); + } +}