(reqUrl string,
reqParams map[string]interface{}, contentType string, files []UploadFile, headers map[string]string)
| 249 | } |
| 250 | |
| 251 | func (target HttpClient) post(reqUrl string, |
| 252 | reqParams map[string]interface{}, contentType string, files []UploadFile, headers map[string]string) ( |
| 253 | string, error) { |
| 254 | requestBody, realContentType := getReader(reqParams, contentType, files) |
| 255 | httpRequest, err := http.NewRequest("POST", reqUrl, requestBody) |
| 256 | if err != nil { |
| 257 | return "", err |
| 258 | } |
| 259 | |
| 260 | // client |
| 261 | httpClient := getClient(reqUrl, target.TimeOut) |
| 262 | |
| 263 | // 添加请求头 |
| 264 | httpRequest.Header.Add("Content-Type", realContentType) |
| 265 | for k, v := range headers { |
| 266 | httpRequest.Header.Add(k, v) |
| 267 | } |
| 268 | |
| 269 | // 发送请求 |
| 270 | SmartIDELog.Debug(formatRequest(httpRequest, reqParams)) |
| 271 | httpResponse, err := httpClient.Do(httpRequest) |
| 272 | if err != nil { |
| 273 | return "", err |
| 274 | } |
| 275 | responseBody, err := parsingResponse(httpResponse) |
| 276 | if err != nil { |
| 277 | return "", err |
| 278 | } |
| 279 | defer httpResponse.Body.Close() |
| 280 | if httpResponse.StatusCode != 200 { |
| 281 | return "", errors.New(httpResponse.Status) |
| 282 | } |
| 283 | //responseBody, err := io.ReadAll(httpResponse.Body) |
| 284 | if len(string(responseBody)) == 0 { |
| 285 | return "", errors.New("reponse body is empty") |
| 286 | } |
| 287 | return string(responseBody), err |
| 288 | |
| 289 | } |
| 290 | |
| 291 | func getClient(url string, timeout time.Duration) *http.Client { |
| 292 | _httpClient := &http.Client{ |
no test coverage detected