如何使用 laravel 中的不记名令牌从 api 请求中获取响应?
How Can I get responses from api request using brearer token in laravel?
问题描述:
- 我有一个啤酒 api。我只需要使用控制器传递请求
实际上发出请求并接收响应。 (注意:
不使用 Postman )
我的 API root 是这样的:http://localhost/cloud/website/api/v1/
在api.php
Route::group(['prefix' => 'v1'], function() {
Route::group(['middleware' => 'auth:sanctum'], function() {
Route::post('entries/sync-info', [EntrySyncInfoController::class, 'index']);
}
}
我正在尝试从 API 获得响应,但一直收到 500 服务器错误。
$token ='24|cbG3w1ONkxxeUBhLCGwFjOSLk......';
$response = $client->request('GET', 'http://localhost/cloud/website/api/v1/entries?perPage=50', [
'headers' => [
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json',
],
]);
return $response->body();
但是当我在 Postman 上请求时,它给了我完美的回应。
postman request's image is here
另外,当我直接在浏览器上点击这个 url 时,它说同样的错误。
我也关注 issue。它 returns 无效。
我该如何解决这个问题?如何使用不记名令牌获得响应?
$response = $client->get('http://localhost/cloud/website/api/v1/entries?perPage=50', [
'headers' => [
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json',
],
]);
参考: 看到这个 post,
也看看 this
问题描述:
- 我有一个啤酒 api。我只需要使用控制器传递请求 实际上发出请求并接收响应。 (注意: 不使用 Postman )
我的 API root 是这样的:http://localhost/cloud/website/api/v1/
在api.php
Route::group(['prefix' => 'v1'], function() {
Route::group(['middleware' => 'auth:sanctum'], function() {
Route::post('entries/sync-info', [EntrySyncInfoController::class, 'index']);
}
}
我正在尝试从 API 获得响应,但一直收到 500 服务器错误。
$token ='24|cbG3w1ONkxxeUBhLCGwFjOSLk......';
$response = $client->request('GET', 'http://localhost/cloud/website/api/v1/entries?perPage=50', [
'headers' => [
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json',
],
]);
return $response->body();
但是当我在 Postman 上请求时,它给了我完美的回应。
postman request's image is here
另外,当我直接在浏览器上点击这个 url 时,它说同样的错误。
我也关注
我该如何解决这个问题?如何使用不记名令牌获得响应?
$response = $client->get('http://localhost/cloud/website/api/v1/entries?perPage=50', [
'headers' => [
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json',
],
]);
参考: 看到这个 post, 也看看 this