【问题标题】:Request to API works in Postman but not when I try with laravel/guzzle对 API 的请求在 Postman 中有效,但在我尝试使用 laravel/guzzle 时无效
【发布时间】:2018-08-29 21:16:48
【问题描述】:

我创建了一个 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
        ]
]);

我明白了:

对象

数据:调试:类: “Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException” 文件:“/home/user_name/public_html/vendor/dingo/api/src/Auth/Auth.php” 行:113

有什么问题?为什么在 Postman 中可以工作,但不能在 Laravel/Guzzle 库中工作?

【问题讨论】:

    标签: laravel api postman guzzle dingo-api


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2019-11-28
      • 2017-05-03
      • 2022-10-31
      • 1970-01-01
      • 2018-10-27
      • 2017-09-14
      • 2021-03-24
      • 2020-04-20
      相关资源
      最近更新 更多