(c *gin.Context)
| 36 | } |
| 37 | |
| 38 | func updateBody(c *gin.Context) { |
| 39 | // 读取请求体 |
| 40 | body, err := io.ReadAll(c.Request.Body) |
| 41 | if err != nil { |
| 42 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse("Failed to read request body")) |
| 43 | c.Abort() |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | // 恢复请求体供后续使用 |
| 48 | c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) |
| 49 | |
| 50 | // 如果请求体为空,直接继续 |
| 51 | if len(body) == 0 { |
| 52 | c.Next() |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | // 解析JSON |
| 57 | var requestData interface{} |
| 58 | if err := json.Unmarshal(body, &requestData); err != nil { |
| 59 | // JSON解析失败,可能不是JSON格式,直接继续 |
| 60 | c.Next() |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | // 解码ID字段 |
| 65 | decodedData := decodeRequestIDs(requestData) |
| 66 | |
| 67 | // 将解码后的数据重新序列化 |
| 68 | decodedBody, err := json.Marshal(decodedData) |
| 69 | if err != nil { |
| 70 | c.JSON(http.StatusInternalServerError, model.SystemError.ToResponse("Failed to encode request body")) |
| 71 | c.Abort() |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | // 更新请求体 |
| 76 | c.Request.Body = io.NopCloser(bytes.NewBuffer(decodedBody)) |
| 77 | c.Request.ContentLength = int64(len(decodedBody)) |
| 78 | } |
| 79 | |
| 80 | func unpdatePath(c *gin.Context) { |
| 81 | // 当路由存在约定的ID参数名时,尝试解码并回填 |
no test coverage detected