Path: blob/1.0-develop/app/Http/Middleware/Api/IsValidJson.php
10277 views
<?php12namespace Pterodactyl\Http\Middleware\Api;34use Illuminate\Http\Request;5use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;67class IsValidJson8{9/**10* Throw an exception if the request should be valid JSON data but there is an error while11* parsing the data. This avoids confusing validation errors where every field is flagged and12* it is not immediately clear that there is an issue with the JSON being passed.13*/14public function handle(Request $request, \Closure $next): mixed15{16if ($request->isJson() && !empty($request->getContent())) {17try {18json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);19} catch (\JsonException $exception) {20throw new BadRequestHttpException('The JSON data passed in the request appears to be malformed: ' . $exception->getMessage());21}22}2324return $next($request);25}26}272829