ragflow http请求 header处理

This commit is contained in:
2026-01-13 11:08:17 +08:00
parent 830d07c23d
commit a101d60df8
2 changed files with 25 additions and 42 deletions

View File

@@ -7,10 +7,9 @@ import (
"sync"
"sync/atomic"
"github.com/gogf/gf/v2/encoding/gjson"
"gitee.com/red-future---jilin-g/common/http"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
)
var (
@@ -150,50 +149,26 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
}
// 创建新的HTTP客户端实例避免共享状态
client := g.Client()
client.SetHeader("Authorization", "Bearer "+c.APIKey)
client.SetHeader("Content-Type", "application/json")
var response *gclient.Response
var headers = make(map[string]string)
headers["Authorization"] = "Bearer " + c.APIKey
headers["Content-Type"] = "application/json"
switch method {
case "GET":
response, err = client.Get(ctx, fullURL, body)
err = http.Get(ctx, fullURL, headers, result, body)
case "POST":
response, err = client.Post(ctx, fullURL, body)
err = http.Post(ctx, fullURL, headers, result, body)
case "PUT":
response, err = client.Put(ctx, fullURL, body)
err = http.Put(ctx, fullURL, headers, result, body)
case "DELETE":
// DELETE请求需要明确使用ContentJson发送body
if body != nil {
response, err = client.ContentJson().Delete(ctx, fullURL, body)
err = http.Delete(ctx, fullURL, headers, result, body)
} else {
response, err = client.Delete(ctx, fullURL)
err = http.Delete(ctx, fullURL, headers, result)
}
default:
return gerror.Newf("unsupported method: %s", method)
}
if err != nil {
g.Log().Errorf(ctx, "RAGFlow HTTP请求失败: %v", err)
return gerror.Wrapf(err, "HTTP request to RAGFlow failed")
}
if response == nil {
return gerror.New("HTTP response is nil")
}
defer response.Close()
// 读取响应体
respBytes := response.ReadAll()
g.Log().Infof(ctx, "RAGFlow响应: %s", string(respBytes))
// 解析JSON到result
if err = gjson.DecodeTo(respBytes, result); err != nil {
g.Log().Errorf(ctx, "RAGFlow响应解析失败: %v, 原始响应: %s", err, string(respBytes))
return gerror.Wrapf(err, "failed to decode RAGFlow response")
}
return
}