How to check request is axios in laravel?

by filiberto , in category: PHP Frameworks , 5 months ago

How to check request is axios in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 5 months ago

@filiberto 

To check if a request is made using Axios in Laravel, you can use the following code in your controller or middleware:

1
2
3
4
5
6
if ($request->headers->get('X-Requested-With') == 'XMLHttpRequest' && $request->headers->get('User-Agent') == 'axios') {
    // Request is made using Axios
    // You can now perform additional checks or operations
} else {
    // Request is not made using Axios
}


This code snippet checks if the request has the 'X-Requested-With' header with the value 'XMLHttpRequest' and the 'User-Agent' header with the value 'axios'. This combination is commonly used by Axios to make requests.