How to Set Headers in Laravel 6 Example?

Today, We want to share with you How to Set Headers in Laravel 6 Example?.In this post we will show you Adding Custom Header with Response in Laravel, hear for how to set header in laravel api we will give you demo and example for implement.In this post, we will learn about laravel set response header in controller with an example.

How to Set Headers in Laravel 6 Example?

There are the Following The simple About Laravel Resources Setting Custom Headers Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to set header for all requests in route group, so the How to create and check custom header with middleware for REST API in Laravel 6 ? is used for this example is following below.

Adding Custom Header with Response in Laravel

Set Headers in Laravel Example:

// resulting a PDF files
header('Content-Type: application/pdf');

// It will be called product_invoces_2021.pdf
header('Content-Disposition: attachment; filename="product_invoces_2021.pdf"');

// The product_invoces_2021 PDF source is in main_original.pdf
readfile('product_invoces_2021.pdf');

set header for all requests in route group

   public function handle($request, Closure $next)
    {
        $acceptHeader = $request->header('Accept');
        if ($acceptHeader != 'application/json') {
            return response()->json([], 400);
        }

        return $next($request);
    }

laravel response headers

return response($content)
    ->withHeaders([
        'Content-Type' => $type,
        'X-Header-One' => 'Header Value',
        'X-Header-Two' => 'Header Value',
    ]);
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Handling CORS in a Laravel application.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment