Neok
Neok

Reputation: 241

Laravel - Send post data with guzzle

I am trying to send post data and retrieve them in the response. I look online and found guzzle, so there is what I've done :

The controller part I wan't to call on route 'test' :

public function test(Request $request) {
    return $request->input('test');
}

public function sinistre(Client $client) {
    $request = $client->post(route('test') , [], [
        'form_params' => [
            'test' => 'edf'
        ]
    ]);

    $response = $request->send();

    dd($response);

    return "ok";
}

Version of guzzle : "guzzlehttp/guzzle": "^6.2"

For now I only got a 500 error response.

Upvotes: 1

Views: 917

Answers (1)

Ian
Ian

Reputation: 3666

Laravel requires a CSRF token to be sent along with the request as it is a post request, so you can either exclude it or get a new token by using csrf_token()

Optionally as per the demo, you can exclude URI's from needing it

    <?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'test/*',
    ];
}

You would of course need to update the $except URI with yours.

Upvotes: 3

Related Questions