php - Laravel REST API 请求对象为空

标签 php laravel ubuntu nginx

环境

我使用 Laravel 5.7 创建了一个应用程序并实现了一个 REST API。我在 routes/api.php 中有一条路由会触发一个中间件,该中间件检查传入请求是否具有名为 api_token 的参数。

这是一个生产环境,以下是具体信息:

  • Linux Ubuntu 18.04 LTS 64 位
  • nginx/1.14.0
  • Laravel 5.7
  • PHP 7.2

.env 文件中的 APP_ENV 设置为“production”,APP_DEBUG 设置为“false”。

问题

我的问题是传入的请求对象在到达服务器时始终为空。至少我的 Laravel 应用程序是这么说的。

这些是我在 routes/api.php 中的路由:

Route::middleware('rest')->group(function() {
    Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');
    Route::get('device-location/all', 'PositionDataController@getAllLocations');
    Route::post('device-location', 'PositionDataController@storeDeviceLocation');
    Route::put('device-location/{deviceID}', 'PositionDataController@updateDeviceLocation');
    Route::delete('device-location/{deviceID}', 'PositionDataController@deleteDeviceLocation');
});

如您所见,路由位于名为“rest”的中间件组中。我正在使用 Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation');用于测试功能的路由。

这是来自中间件的代码:

public function handle($request, Closure $next)
{
    if(request()->all()) {
        $deviceID = request()->device_id;
    }
    else {
        return response()->json([
            'error' => 'The request object is empty.',
            'request' => request(),
            'parameters' => request()->all(),
            'content' => request()->getContent(),
            'input' => request()->input()
        ], 500);
    }

    $device = MobileDevice::where('device_id', $deviceID)->first();

    if($device) {
        $deviceToken = $device->api_token;
        if($deviceToken == request()->api_token) {
            return $next($request);
        }
        else {
            return response()->json(['error' => 'Token does not match.'], 500);
        }
    }
    else {
        return response()->json(['error' => 'The device with the id [' . $deviceID . '] could not be found.'], 500);
    }
}

中间件首先检查请求对象中是否有参数,然后执行一些逻辑检查是否发送了正确的 token 。如果请求对象为空,它会返回一些数据以帮助我了解出了什么问题。

我使用 Postman ( https://www.getpostman.com) 来测试 API。这是我的 Postman 设置:

postman 设置

Screen Shot

postman 标题

Screen Shot

这是我在 Postman 中得到的响应:

postman 回复

Screen Shot

如果我在浏览器中调用该路由,我会得到相同的结果。 无论我是否输入参数,请求似乎总是空的。

以下是我尝试做的事情:

  • 不使用中间件
  • 使用 $request 变量代替助手 request()
  • 在我的 Postman 设置的 Headers 中切换“application/json”和“application/x-www-form-urlencoded”
  • 在浏览器中调用该路由
  • 更新到 Laravel 5.7

奇怪的是,所有这些在我的本地环境和与生产服务器具有相同规范的测试服务器上都能完美运行。

更新 01:

所以好像更糟了...

如果我在 web.php 中添加这样的路由:

Route::get('/request-return', function() {
    return request()->all();
});

然后像这样访问那条路线:

laravel.application/request-return?device_id=1&api_token=XgkQLs8G7OYTqdwsXmjJT5G9bxv20DRi

我得到一个空数组 []。

所以看起来参数没有到达服务器本身。

最佳答案

您正在通过 GET 请求获取设备 ID,因此请使用下面的行而不是 $request()->device_id

用这个让我知道 $name = $request->input('device_id')

关于php - Laravel REST API 请求对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53867400/

相关文章:

javascript - 合并标题并针对每个页面进行修改

php - 为什么我的 19 位数字会四舍五入为 17 位数字

php - 使用 php 将图像的 dpi 从 72 更改为 96

angular - 我如何调用 laravel 护照的忘记密码并使用 angular 8 验证 api?

c++ - 如何在 JUCE 项目中获得 "go to definition"?

php - 页面点击计数器 - 我是否过度使用了数据库?

php - Laravel Cookie 未通过 Javascript 显示

php - 开发与生产中的 Laravel "universal to"

ubuntu - ffmpeg:视频编解码器 ansi 与 flv 不兼容

linux - 我在哪里可以找到 libnautilus 的文档?