From dbffda69fda6d97c0246a07e13f61bec5b098289 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 13 Oct 2023 17:44:02 +0800 Subject: [PATCH] fix --- .../internal/logic/uploadfilebackendlogic.go | 3 +- utils/curl/http_curl.go | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/server/upload/internal/logic/uploadfilebackendlogic.go b/server/upload/internal/logic/uploadfilebackendlogic.go index 6bd578e1..1323304c 100644 --- a/server/upload/internal/logic/uploadfilebackendlogic.go +++ b/server/upload/internal/logic/uploadfilebackendlogic.go @@ -1,7 +1,6 @@ package logic import ( - "fmt" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/file" @@ -63,7 +62,7 @@ func (l *UploadFileBackendLogic) UploadFileBackend(req *types.UploadFileBackendR l.r.ParseMultipartForm(32 << 20) fileObject, _, err := l.r.FormFile("file") - fmt.Printf("%#v", fileObject) + //fmt.Printf("%#v", fileObject) if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,no files") diff --git a/utils/curl/http_curl.go b/utils/curl/http_curl.go index 0fbef756..e704569d 100644 --- a/utils/curl/http_curl.go +++ b/utils/curl/http_curl.go @@ -1,6 +1,8 @@ package curl import ( + "bufio" + "bytes" "errors" "io" "net/http" @@ -32,3 +34,48 @@ func ApiCall(url, method string, header map[string]string, body io.Reader, timeO } return client.Do(requestHandle) } + +// 请求(读取返回字节内容) +func ApiCall2(url, method string, header map[string]string, body io.Reader, timeOut time.Duration) (result []byte, err error) { + method = strings.ToUpper(method) + if method != "GET" && method != "POST" && method != "PUT" && method != "DELETE" { + return nil, errors.New("invalid http method") + } + if url == "" { + return nil, errors.New("request url can`t be empty") + } + client := &http.Client{} + if timeOut <= 0 { + client.Timeout = time.Second * 15 + } else { + client.Timeout = timeOut + } + requestHandle, err := http.NewRequest(method, url, body) + if err != nil { + return nil, err + } + for k, v := range header { + requestHandle.Header.Set(k, v) + } + rsp, err := client.Do(requestHandle) + if err != nil { + return nil, err + } + defer rsp.Body.Close() + buff := bytes.Buffer{} + reader := bufio.NewReader(rsp.Body) + for { + line, _, err := reader.ReadLine() + if err == io.EOF { + break + } + if err != nil { + return nil, err + } + _, err = buff.Write(line) + if err != nil { + return nil, err + } + } + return buff.Bytes(), nil +}