forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsValidJson.php
More file actions
31 lines (27 loc) · 1018 Bytes
/
IsValidJson.php
File metadata and controls
31 lines (27 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
namespace Pterodactyl\Http\Middleware\Api;
use Closure;
use JsonException;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class IsValidJson
{
/**
* Throw an exception if the request should be valid JSON data but there is an error while
* parsing the data. This avoids confusing validation errors where every field is flagged and
* it is not immediately clear that there is an issue with the JSON being passed.
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($request->isJson() && !empty($request->getContent())) {
try {
json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
throw new BadRequestHttpException('The JSON data passed in the request appears to be malformed: ' . $exception->getMessage());
}
}
return $next($request);
}
}