简体   繁体   中英

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

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()
        ]
    ]);

If this is remote laravel server, CSRF tokens can't be expected.

If you want to handle your post request, either pass right token or remove CSRF checking from middleware.

You can do it, by including your route to

protected $except 

in your /app/Http/Midddleware/VerifyCsrfToken.php file...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM