Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Middleware/Api/IsValidJson.php
10277 views
1
<?php
2
3
namespace Pterodactyl\Http\Middleware\Api;
4
5
use Illuminate\Http\Request;
6
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
7
8
class IsValidJson
9
{
10
/**
11
* Throw an exception if the request should be valid JSON data but there is an error while
12
* parsing the data. This avoids confusing validation errors where every field is flagged and
13
* it is not immediately clear that there is an issue with the JSON being passed.
14
*/
15
public function handle(Request $request, \Closure $next): mixed
16
{
17
if ($request->isJson() && !empty($request->getContent())) {
18
try {
19
json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
20
} catch (\JsonException $exception) {
21
throw new BadRequestHttpException('The JSON data passed in the request appears to be malformed: ' . $exception->getMessage());
22
}
23
}
24
25
return $next($request);
26
}
27
}
28
29