PASSWORD RESET

Your destination for complete Tech news

How to get headers from Laravel Request object?

888 0
< 1 min read

To get the headers from a Laravel Request object, you can use the headers method of the Request object.

Here is an example of how to get the headers from a Laravel Request object:

use Illuminate\Http\Request;

Route::get('/', function (Request $request) {
    $headers = $request->headers;

    foreach ($headers as $header => $value) {
        echo "$header: $value<br>";
    }
});

In this example, the headers method of the Request object returns an instance of the Symfony\Component\HttpFoundation\HeaderBag class, which implements the IteratorAggregate interface and can be iterated over like an array.

You can also use the header() method of the Request object to get the value of a specific header. For example:

$userAgent = $request->header('User-Agent');
echo $userAgent;

This code gets the value of the User-Agent header from the Request object and assigns it to the $userAgent variable.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.