package knowledge import ( "bytes" "eta_gn/eta_bridge/global" "eta_gn/eta_bridge/utils" "io" "mime/multipart" "net/http" "strconv" "time" "github.com/gin-gonic/gin" ) type KnowledgeUploadResp struct { Ret int Msg string ErrMsg string `json:"-"` Success bool `description:"true 执行成功,false 执行失败"` } func ForwardRequest(c *gin.Context, targetURL string) (responseBody []byte, err error) { // 获取文件 form, err := c.MultipartForm() if err != nil { return } // 将文件数据重新封装为multipart/form-data格式 body := new(bytes.Buffer) writer := multipart.NewWriter(body) // 将原请求的文件添加到新的multipart请求体中 for _, files := range form.File { for _, file := range files { part, er := writer.CreateFormFile(file.Filename, file.Filename) if er != nil { err = er return } // 打开文件并写入到multipart的部分 fileContent, er := file.Open() if er != nil { err = er return } _, er = io.Copy(part, fileContent) if er != nil { err = er return } } } // 将body的内容写入 for k, v := range c.Request.PostForm { for _, value := range v { writer.WriteField(k, value) } } writer.Close() // 2. 构造一个新的请求并转发 req, err := http.NewRequest(c.Request.Method, targetURL, body) if err != nil { return } // 设置Content-Type为multipart/form-data req.Header.Set("Content-Type", writer.FormDataContentType()) nonce := utils.GetRandStringNoSpecialChar(32) timeStamp := time.Now().UnixMilli() / 1000 signature := utils.GetSign(global.CONFIG.EtaReport.AppId, global.CONFIG.EtaReport.Secret, nonce, strconv.Itoa(int(timeStamp))) req.Header.Set("nonce", utils.GetRandStringNoSpecialChar(32)) req.Header.Set("timestamp", strconv.Itoa(int(timeStamp))) req.Header.Set("appid", global.CONFIG.EtaReport.AppId) req.Header.Set("signature", signature) // 3. 执行请求并处理响应 client := &http.Client{} resp, err := client.Do(req) if err != nil { return } defer resp.Body.Close() // 将目标服务的响应写回给客户端 responseBody, err = io.ReadAll(resp.Body) if err != nil { return } return }