go-platform/docs/快速修复-请求体为空.md

123 lines
2.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 快速修复:请求体为空问题
## 问题
```
POST /platform/qiniu/save 400
{"code": 400, "msg": "参数解析失败: 请求体为空"}
```
## 快速修复步骤
### 1. 重启后端服务(已修改配置)
```bash
systemctl restart go-api
```
### 2. 查看服务状态
```bash
systemctl status go-api
```
预期输出:
```
● go-api.service - Go API Server
Loaded: loaded
Active: active (running)
```
### 3. 查看日志
```bash
tail -f /www/wwwroot/api.yunzer.cn/go.log
```
### 4. 测试上传
1. 登录系统
2. 进入软件升级页面
3. 上传一个文件
### 5. 观察日志输出
应该看到:
```
SaveFileRecord 请求体长度: xxx
SaveFileRecord 请求体内容: {"key":"...","hash":"...","size":...}
```
## 已修改的文件
`go/main.go` - 添加 `beego.BConfig.CopyRequestBody = true`
`go/conf/app.conf` - 添加 `copyrequestbody = true`
`go/controllers/qiniu_upload.go` - 添加调试日志
## 如果还是失败
### 检查 1: 服务是否重启成功
```bash
systemctl status go-api
```
如果失败,查看错误:
```bash
journalctl -u go-api -n 50
```
### 检查 2: 配置是否生效
查看日志中是否有请求体内容输出。如果没有,说明配置未生效。
### 检查 3: 前端请求是否正确
打开浏览器开发者工具,查看 Network 标签:
- 请求方法POST
- Content-Type: application/json
- 请求体:应该有 JSON 数据
## 完整上传流程
```
1. 前端上传文件到七牛云 ✓
2. 七牛云返回文件信息 ✓
{
"key": "2026/04/09/xxx.exe",
"hash": "xxx",
"size": 60742452
}
3. 前端调用 /platform/qiniu/save ← 这里失败了
POST /platform/qiniu/save
Body: {
"key": "...",
"hash": "...",
"size": ...,
"name": "...",
"mimeType": "...",
"cate": 0
}
4. 后端保存到数据库 ← 修复后应该成功
5. 返回文件 URL
```
## 修复原理
Beego 框架默认不复制请求体,需要启用 `CopyRequestBody`
```go
// 修复前
c.Ctx.Input.RequestBody // 空的
// 修复后(启用 CopyRequestBody
c.Ctx.Input.RequestBody // 包含请求体数据
```
## 更新时间
2026-04-09