-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilHttp.go
158 lines (144 loc) · 3.84 KB
/
utilHttp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2025 minRAG Authors.
//
// This file is part of minRAG.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses>.
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
)
// httpPostJsonBody 使用Post发送Json请求
func httpPostJsonBody(client *http.Client, authorization string, url string, header map[string]string, bodyMap map[string]interface{}) ([]byte, error) {
resp, err := httpPostJsonResponse(client, authorization, url, header, bodyMap)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// 读取响应体内容
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if len(body) < 1 {
return nil, errors.New("body is empty")
}
return body, nil
}
// httpPostJsonResponse post请求的response
func httpPostJsonResponse(client *http.Client, authorization string, url string, header map[string]string, bodyMap map[string]interface{}) (*http.Response, error) {
if client == nil {
return nil, errors.New("httpClient is nil")
}
// 序列化请求体
payloadBytes, err := json.Marshal(bodyMap)
if err != nil {
return nil, err
}
// 创建HTTP请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
// 设置请求头
if authorization != "" {
req.Header.Set("Authorization", "Bearer "+authorization)
}
req.Header.Set("Content-Type", "application/json")
if len(header) > 0 {
for k, v := range header {
req.Header.Set(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return resp, err
}
// 保留错误信息
//if resp.StatusCode >= 400 {
// return nil, fmt.Errorf("HTTP error: %s", resp.Status)
//}
// 检查状态码
if resp.StatusCode != http.StatusOK {
bodyByte, _ := io.ReadAll(resp.Body)
resp.Body.Close()
return nil, errors.New(string(bodyByte))
}
return resp, err
}
// httpUploadFile http上传附件
func httpUploadFile(client *http.Client, method string, url string, filePath string, header map[string]string) ([]byte, error) {
if client == nil {
return nil, errors.New("httpClient is nil")
}
if filePath == "" {
return nil, errors.New("filePath is empty")
}
if method == "" {
method = "POST"
}
// 打开文件
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
// 创建请求体
bodyBuffer := &bytes.Buffer{}
if _, err := io.Copy(bodyBuffer, file); err != nil {
return nil, fmt.Errorf("read file error: %w", err)
}
// 创建请求
req, err := http.NewRequest(method, url, bodyBuffer)
if err != nil {
return nil, err
}
if len(header) > 0 {
for k, v := range header {
req.Header.Set(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// 关闭resp.Body
defer resp.Body.Close()
// 保留错误信息
//if resp.StatusCode >= 400 {
// return nil, fmt.Errorf("HTTP error: %s", resp.Status)
//}
// 读取响应体内容
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if len(body) < 1 {
return nil, errors.New("body is empty")
}
// 保留错误信息
//if resp.StatusCode >= 400 {
// return nil, fmt.Errorf("HTTP error: %s", resp.Status)
//}
// 检查状态码
if resp.StatusCode != http.StatusOK {
return nil, errors.New(string(body))
}
return body, nil
}