Home » Php » In Laravel, how do I make a guzzle http post request with csrf token

In Laravel, how do I make a guzzle http post request with csrf token

Posted by: admin October 29, 2018 Leave a comment

Questions:

I want to make an http post to a server URL using GuzzleHTTP. My application is built with Laravel and the guzzle request requires a CSRF token. I’ve tried a few ways to include it but it isn’t working. What is the proper way to include the CSRF token in a guzzle request? Here is my code:

    use GuzzleHttp\Client;

    $client = new Client();
    $response = $client->post(url('/import'), [
        'headers' => [
            'X-CSRF-Token' => csrf_token()
        ]
    ]);
Answers: