user10814616
user10814616

Reputation: 41

Laravel REST API request object is empty

Environment

I created an application using Laravel 5.7 and implemented a REST API. I have a route in routes/api.php that triggers a middleware which checks if the incoming request has a parameter called api_token.

This is a production environment and here are the specifics:

APP_ENV in the .env file is set to 'production' and APP_DEBUG is set to 'false'.

Problem

My problem is that the incoming request object is always empty when it arrives at the server. At least that's what my Laravel application says.

These are my routes in 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');
});

The routes are in a middleware group called 'rest' as you can see. I'm using the Route::get('device-location/{deviceID}', 'PositionDataController@getDeviceLocation'); route to test the functionality.

Here's the code from the middleware:

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);
    }
}

The middleware first checks if there are parameters in the request object and then does some logic to check if the right token was sent. If the request object is empty it returns some data to help me understand what went wrong.

I use Postman (https://www.getpostman.com) to test the API. Here's my Postman setup:

Postman setup

Screen Shot

Postman headers

Screen Shot

This is the response I get in Postman:

Postman response

Screen Shot

I get the same result if I call that route in a browser. Regardless of if I put in parameters or not the request seems to be always empty.

Here are the things that I've tried to do:

The strange thing is that all of this works perfectly on my local environment and on a test server that has the same specs as the production server.

UPDATE 01:

So it seems to be even worse...

If I add a route like this in web.php:

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

and visit that route like this:

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

I get back an empty array [].

So it seems like the parameters don't get to the server itself.

Upvotes: 4

Views: 8370

Answers (2)

user10814616
user10814616

Reputation: 41

Okay I could solve the problem. It didn't have anything to do with Laravel. It was a nginx configuration problem:

https://serverfault.com/questions/231578/nginx-php-fpm-where-are-my-get-params

Upvotes: 0

Googlian
Googlian

Reputation: 6723

You are getting device id through GET request so use the below line instead of $request()->device_id.

Use this and let me know $name = $request->input('device_id')

Upvotes: 1

Related Questions