一、报错
**<html><meta charset="utf-8" /><title>json格式错误</title><div>请传递正确的json参数</div></html>\n**
这种情况是宝塔上面的免费防火墙导致的,这边我关闭了Nginx免费防火墙就好了,也可以修改规则,这里嫌麻烦就没修改了
二、报错
Invalid JSON data in request body: Control character error, possibly incorrectly encoded
这是hyperf底层代码返回的错误,找到命名空间为namespace Hyperf\HttpMessage\Server; 的文件中的 normalizeParsedBody 方法
protected static function normalizeParsedBody(array $data = [], ?ServerRequestInterface $request = null): array
{
if (! $request) {
return $data;
}
$rawContentType = $request->getHeaderLine('content-type');
if (($pos = strpos($rawContentType, ';')) !== false) {
// e.g. text/html; charset=UTF-8
$contentType = strtolower(substr($rawContentType, 0, $pos));
} else {
$contentType = strtolower($rawContentType);
}
try {
$parser = static::getParser();
if ($parser->has($contentType) && $content = (string) $request->getBody()) {
//这一段话就是加入的接收json的gzip
if (str_contains($request->getHeaderLine('Content-Encoding'), 'gzip')) {
// 如果有gzip请求头,对请求content进行gz解压
$content = gzdecode($content);
}
$data = $parser->parse($content, $contentType);
}
} catch (InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage(), request: $request);
} catch (BadRequestHttpException $exception) {
throw $exception->setRequest($request);
}
return $data;
}
小伙伴们!这样就完成了哦! QQ学习交流群:842167453 ,欢迎小伙伴加入一起学习。