I am using Slim v3 and the json schema validator by justinrainbow for my API. What I want to do and just can't get to work is:
- in the middleware: validate incoming json with defaults. this produces a modified object
- write the modified object back into the request, so that it can be processed by the core controllers
What I am failing at is:
# inside middleware:
$requestbody = $request->getBody();
$requestobject = json_decode($requestbody);
# validation and modification of $requestobject takes place here
$request->getBody()->write(json_encode($requestobject));
$request->reparseBody();
return $next($request, $response);
From that point on, the request body is just null
. What am I doing wrong? I am rather certain that there is something wrong with the way I am modifying Slim objects, because it does not work when I manually try $request->getBody()->write('{"some": "content"}')
as well.