简体   繁体   中英

Request to API works in Postman but not when I try with laravel/guzzle

I make an API and now I try to access to that API. WIth Postman everything is perfect and works fine:

在此处输入图片说明

but when I try the same with guzzle/laravel with code:

$res3 = $client3->post('https://app.EXAMPLE.com/api/update/'.$serial, [
    'headers' => [
        'Authorization' => 'Bearer '.$res2['token'],
        'Content-Type' => 'application/json'
    ],

    'form_params' => [

      'token' => $res2['token'],
        'bookingdate' => '07/07/2018 12:00 am',
        'notes' => $SpecialRequests
        ]
]);

I got:

Object

data : debug : class : "Symfony\\Component\\HttpKernel\\Exception\\UnauthorizedHttpException" file : "/home/user_name/public_html/vendor/dingo/api/src/Auth/Auth.php" line : 113

What is a problem? WHy works in Postman but won't work with Laravel/Guzzle library?

In Postman, it looks like you are sending the token as a request parameter. In your code, it looks like you are using form_params, which are application/x-www-form-urlencoded.

Try this: http://docs.guzzlephp.org/en/stable/quickstart.html#query-string-parameters

$res3 = $client3->post('https://app.EXAMPLE.com/api/update/'.$serial, [ 'headers' => [ 'Authorization' => 'Bearer '.$res2['token'], 'Content-Type' => 'application/json' ], 'query' => [ 'token' => $res2['token'], 'bookingdate' => '07/07/2018 12:00 am', 'notes' => $SpecialRequests ] ]);

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