Aleks Per
Aleks Per

Reputation: 1639

Laravel Guzzle parameters

From the API docs I have this curl request:

curl https://api.example.com/api/Jwt/Token ^
        -d Username="asd%40gmail.com" ^
        -d Password="abcd1234"

Now I'm trying to create that request in Laravel 5.1 using the Guzzle library so I wrote:

 public function test()
    {

$client = new GuzzleHttp\Client();

$res = $client->createRequest('POST','https://api.example.com/api/Jwt/Token', [

    'form_params' => [
        'Username' => 'asd%40gmail.com',
        'Password' => 'abcd1234'
]
            ]);

$res = json_decode($res->getBody()->getContents(), true);

dd ($res);
}

But I get this error:

***ErrorException in Client.php line 126:
Argument 3 passed to GuzzleHttp\Client::request() must be of the type array,     string given, called in     /home/ibook/public_html/vendor/guzzlehttp/guzzle/src/Client.php on line 87 and defined***

WHat is the problem and how do I solve that error?

p.s. I also tried

$res = $client->createRequest('POST','https://api.example.com/api/Jwt/Token', 

    'form_params' => [
        'Username' => 'asd%40gmail.com',
        'Password' => 'abcd1234'

            ]);

But then I get:

syntax error, unexpected '=>' (T_DOUBLE_ARROW)

Upvotes: 0

Views: 2415

Answers (1)

Mozammil
Mozammil

Reputation: 8750

You are calling the createRequest function instead of request. This should work:

$response = $client->request('POST', 'https://api.example.com/api/Jwt/Token', [
    'form_params' => [
        'Username' => 'asd%40gmail.com',
        'Password' => 'abcd1234'
    ]
]);

Check the documentation

Upvotes: 5

Related Questions