Jsonify
Not limited to POST — any body-carrying method (POST,
PUT, PATCH, DELETE) is parsed. Once it runs,
$request->json() reads its pre-parsed data bag; the behavior is identical to
Request's own lazy parsing either way (see
Request) — Jsonify only adds media-type detection, a body-size
limit, and structured error responses on top.
Wiring it
final class Jsonify extends \Monad\Clarity\Middlewares\Jsonify
{
public function __construct()
{
parent::__construct(); // override args below once the API's real requirements are known
}
}
Route::post('/orders', [OrderController::class, 'store'])->middleware(Jsonify::class);
new Jsonify(
bool $requireJsonContentType = false, // true => a non-JSON body-carrying request gets 415 instead of passing through unparsed
bool $requireObjectTopLevel = false, // true => only `{...}` bodies accepted, not a bare array/string/number
int $maxBodyBytes = 1_048_576, // 1 MB default
int $maxDepth = 512,
)
A real request through it
// POST /orders, Content-Type: application/json, body: {"sku":"WIDGET-1","qty":3}
function (Request $request): Response {
return Response::json(['sku' => $request->json('sku'), 'qty' => $request->json('qty')]);
}
// => 200 {"sku":"WIDGET-1","qty":3}
A malformed body:
// body: {not valid json
// => 400 {"error":"Request body is not valid JSON: Syntax error"}
Next steps
- Request —
json()'s full contract, with or without Jsonify in the pipeline.