(reqParams map[string]interface{}, contentType string, files []UploadFile)
| 303 | } |
| 304 | |
| 305 | func getReader(reqParams map[string]interface{}, contentType string, files []UploadFile) (io.Reader, string) { |
| 306 | if strings.Contains(contentType, "json") { |
| 307 | bytesData, _ := json.Marshal(reqParams) |
| 308 | return bytes.NewReader(bytesData), contentType |
| 309 | } else if files != nil { |
| 310 | body := &bytes.Buffer{} |
| 311 | // 文件写入 body |
| 312 | writer := multipart.NewWriter(body) |
| 313 | for _, uploadFile := range files { |
| 314 | file, err := os.Open(uploadFile.Filepath) |
| 315 | if err != nil { |
| 316 | panic(err) |
| 317 | } |
| 318 | part, err := writer.CreateFormFile(uploadFile.Name, filepath.Base(uploadFile.Filepath)) |
| 319 | if err != nil { |
| 320 | panic(err) |
| 321 | } |
| 322 | _, err = io.Copy(part, file) |
| 323 | if err != nil { |
| 324 | SmartIDELog.ImportanceWithError(err) |
| 325 | } |
| 326 | file.Close() |
| 327 | } |
| 328 | // 其他参数列表写入 body |
| 329 | for k, v := range reqParams { |
| 330 | if err := writer.WriteField(k, v.(string)); err != nil { |
| 331 | panic(err) |
| 332 | } |
| 333 | } |
| 334 | if err := writer.Close(); err != nil { |
| 335 | panic(err) |
| 336 | } |
| 337 | // 上传文件需要自己专用的contentType |
| 338 | return body, writer.FormDataContentType() |
| 339 | } else { |
| 340 | urlValues := url.Values{} |
| 341 | for key, val := range reqParams { |
| 342 | urlValues.Set(key, val.(string)) |
| 343 | } |
| 344 | reqBody := urlValues.Encode() |
| 345 | return strings.NewReader(reqBody), contentType |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // formatRequest generates ascii representation of a request |
| 350 | func formatRequest(r *http.Request, reqParams map[string]interface{}) string { |
no test coverage detected