From 7e1fe31085c71b5f02e66f3028bb3aa58b07dd03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 27 Jan 2025 19:25:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20http=E6=94=AF=E6=8C=81json5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/network/http-server.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/onebot/network/http-server.ts b/src/onebot/network/http-server.ts index 39ca7a14..a11f3c46 100644 --- a/src/onebot/network/http-server.ts +++ b/src/onebot/network/http-server.ts @@ -8,6 +8,7 @@ import cors from 'cors'; import { HttpServerConfig } from '@/onebot/config/config'; import { NapCatOneBot11Adapter } from "@/onebot"; import { IOB11NetworkAdapter } from "@/onebot/network/adapter"; +import json5 from 'json5'; export class OB11HttpServerAdapter extends IOB11NetworkAdapter { private app: Express | undefined; @@ -52,12 +53,17 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter this.app.use((req, res, next) => { // 兼容处理没有带content-type的请求 req.headers['content-type'] = 'application/json'; - const originalJson = express.json({ limit: '5000mb' }); - originalJson(req, res, (err) => { - if (err) { + let rawData = ''; + req.on('data', (chunk) => { + rawData += chunk; + }); + req.on('end', () => { + try { + req.body = json5.parse(rawData); + next(); + } catch (err) { return res.status(400).send('Invalid JSON'); } - next(); }); });