rewriteTaskPluginJSONModel patches the top-level JSON "model" field and replaces BodyStorage. Non-JSON bodies are left untouched.
(c *gin.Context, spelling string)
| 13 | // rewriteTaskPluginJSONModel patches the top-level JSON "model" field and |
| 14 | // replaces BodyStorage. Non-JSON bodies are left untouched. |
| 15 | func rewriteTaskPluginJSONModel(c *gin.Context, spelling string) error { |
| 16 | mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type")) |
| 17 | if err != nil { |
| 18 | return nil |
| 19 | } |
| 20 | if mediaType != "application/json" && !strings.HasSuffix(mediaType, "+json") { |
| 21 | return nil |
| 22 | } |
| 23 | storage, err := common.GetBodyStorage(c) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | raw, err := storage.Bytes() |
| 28 | if err != nil { |
| 29 | return err |
| 30 | } |
| 31 | patched, err := ojson.SetBytes(raw, "model", spelling) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | newStorage, err := common.CreateBodyStorage(patched) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | _ = storage.Close() |
| 40 | c.Set(common.KeyBodyStorage, newStorage) |
| 41 | c.Set(common.KeyRequestBody, nil) |
| 42 | c.Request.Body = io.NopCloser(newStorage) |
| 43 | c.Request.ContentLength = int64(len(patched)) |
| 44 | return nil |
| 45 | } |