dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.32k stars 1.25k forks source link

Middleware not woking #1393

Open hex20 opened 7 years ago

hex20 commented 7 years ago

Hi, I'm trying to filter a JSON request using a middleware but It doesn't appear to be working.

A request like this will work just fine

{"username":"username","email":"test@email.com","password":"password","confirm_password":"password"}

But one like this will throw validation errors saying that the fields are required

{"data":{"attributes":{"username":"username","email":"test@email.com","password":"password","confirm_password":"password"},"type":"users"}}

I've got a middleware that is meant to filter out the data and attributes part of the object and leave the content, It looks like this

public function handle($request, Closure $next)
    {
        $input = $request->all();

        if (isset($input['data']['attributes'])) {
            $input = $input['data']['attributes'];

            $request->replace($input);
            \Log::info($request->all());
        }

        return $next($request);
    }

That log you see in the above method logs out the response I'm looking for

[2017-05-22 06:32:03] local.INFO: array (
  'username' => 'username',
  'email' => 'test@email.com',
  'password' => 'password',
  'confirm_password' => 'password',
)  

But I still get the error

{"error":{"message":"422 Unprocessable Entity","errors":{"username":["The username field is required."],"email":["The email field is required."],"password":["The password field is required."],"confirm_password":["The confirm password field is required."]},"status_code":422}}

Does anybody know what would be causing this issue? Any help is greatly appreciated.

meistocko commented 7 years ago

Is your middleware kicking in before the validation?