如何将多部分 POST 请求重定向到 Golang 中的第二台服务器?

How to redirect multipart POST request to a second server in Golang?

我正在尝试执行以下操作。

|Upload file in HTML post file form|
              |
              ⌄
|Server A forwards the multipart request| 
              |                                     
              ⌄                                     
|Server B receives and stores the file from the forwarded multipart request|
              |
              ⌄
|Server A receives response from Server B when Server B is done|

在服务器 A 上处理多部分请求很简单,但是当我尝试在服务器 B 上处理转发的请求时,它失败了 multipart: NextPart: EOF

我正在尝试创建单独的 frontend/backend 服务。前端只处理UI相关处理,而后端实际上会对文件做一些处理,因此需要多部分请求转发。

服务器A上的转发代码如下。 解决方案已从此处获取。

func forwardRequest(address string, path string, r *http.Request) (interface{}, error) {
    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        return nil, err
    }

    r.Body = ioutil.NopCloser(bytes.NewReader(body))
    proxyReq, err := http.NewRequest(r.Method, fmt.Sprintf("%s%s", address, path), bytes.NewReader(body))
    if err != nil {
        return nil, err
    }

    for header, values := range r.Header {
        for _, value := range values {
            proxyReq.Header.Add(header, value)
        }
    }

    client := &http.Client{}
    resp, err := client.Do(proxyReq)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    return resp, nil
}

服务器B上处理转发请求的代码:

func testMultiPart(w http.ResponseWriter, r *http.Request) {
    if err := r.ParseMultipartForm(10 << 20); err != nil {
        err = errors.Wrap(errors.WithStack(err), "Backend: Failed to parse form")
        w.WriteHeader(http.StatusInternalServerError)
        fmt.Fprint(w, fmt.Sprintf("{\"error\":\"%s\"}", err.Error())
        return
    }
}

感谢任何帮助。

我成功了。我相信这只是我自己的错误,没有正确填写 URI。在任何情况下,我都会 post 我的解决方案中的片段以供将来参考。

客户端html文件格式部分:

<form action="/test-main/file-test" enctype="multipart/form-data" method="post">
    <label for="file-upload">Upload your file :</label>
    <input type="file" id="file-upload" name="file-upload" accept="image/*">
</form>

服务器A代码:

import (
    "net/http"
    "errors"
    "fmt"
    "log"
    "io/ioutil"
    "bytes"

    "github.com/gorilla/mux"
)

func fileUpload(w http.ResponseWriter, r *http.Request) {
    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        return log.Fatal(err)
    }

    r.Body = ioutil.NopCloser(bytes.NewReader(body))
    // If Server A and B are separate docker images, you may need to use their docker subnet IP, like below.
    proxyReq, err := http.NewRequest(r.Method, fmt.Sprintf("http://172.18.0.2:8082%s", r.RequestURI), bytes.NewReader(body))
    if err != nil {
        return log.Fatal(err)
    }

    for header, values := range r.Header {
        for _, value := range values {
            proxyReq.Header.Add(header, value)
        }
    }

    client := &http.Client{}
    resp, err := client.Do(proxyReq)
    if err != nil {
        return log.Fatal(err)   
    }
    defer resp.Body.Close()
    respBody, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return log.Fatal(err)
    }

    // Process Server B response
    // ...
}

func createRouter() *mux.Router {
    r := mux.NewRouter()
    testPath := r.PathPrefix("/test-main").Subrouter()
    testPath.HandleFunc("/file-test", fileUpload)
    return r
}

func main() {
    // Create Server and Route Handlers
    srv := &http.Server{
        Handler:      createRouter(),
        Addr:         ":8081",
        ReadTimeout:  30 * time.Second,
        WriteTimeout: 30 * time.Second,
    }

    // Start Server
    go func() {
        log.Println("Starting Server")
        if err := srv.ListenAndServe(); err != nil {
            log.Fatal(err)
        }
    }()
}

和服务器 B 代码:

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"

    "github.com/gorilla/mux"
)

func uploadFile(w http.ResponseWriter, r *http.Request) {
    if err := r.ParseMultipartForm(10 << 20); err != nil {
        log.Fatal(err)
    }

    file, handler, err := r.FormFile("file-upload")
    if err == http.ErrMissingFile {
        return nil
    }

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Uploaded File: %+v\n", handler.Filename)
    fmt.Printf("File Size: %+v\n", handler.Size)
    fmt.Printf("MIME Header: %+v\n", handler.Header)

    defer file.Close()

    // Create file
    dst, err := os.Create(fmt.Sprintf("/some-destination-folder/%s", handler.Filename))
    if err != nil {
        log.Fatal(err)
    }

    // Copy the uploaded file to the created file on the file system.
    if _, err := io.Copy(dst, file); err != nil {
        if err2 := dst.Close(); err2 != nil {
            log.Fatal(err)
        }
        log.Fatal(err)
    }
    dst.Close()

    return nil
}

func (c *Controller) createRouter() *mux.Router {
    r := mux.NewRouter()
    testPath := r.PathPrefix("/test-main").Subrouter()
    testPath.HandleFunc("/file-test", uploadFile)

    return r
}

func main() {
    // Create Server and Route Handlers
    srv := &http.Server{
        Handler:      createRouter(),
        Addr:         ":8082",
        ReadTimeout:  30 * time.Second,
        WriteTimeout: 30 * time.Second,
    }

    // Start Server
    go func() {
        log.Println("Starting Server")
        if err := srv.ListenAndServe(); err != nil {
            log.Fatal(err)
        }
    }()
}

祝未来的读者好运。