Postman可以正常请求API,但在使用laravel/guzzle时无法成功,请问可能是什么原因?

4
我创建了一个API,现在我想要访问这个API。 使用Postman一切都很完美,而且运行良好:
但是当我尝试使用代码guzzle/laravel进行相同的操作时:
$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
        ]
]);

我得到:

对象

数据:debug:class: "Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException" 文件:"/home/user_name/public_html/vendor/dingo/api/src/Auth/Auth.php" 行:113

问题出在哪里?为什么 Postman 可以工作,但在 Laravel/Guzzle 库中不起作用?

1个回答

5
在Postman中,看起来您将令牌作为请求参数发送。在您的代码中,看起来您正在使用form_params,这是application/x-www-form-urlencoded类型的。
尝试这个: 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 ] ]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接