title: How to redirect multipart POST request to a second server in Golang?
body: <p>I am trying to do the following.</p>
  <pre><code>|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|
  </code></pre>
  <p>Processing the multipart request on Server A is straightforward, but when I try to process the forwarded request on Server B it fails with <code>multipart: NextPart: EOF</code>.</p>
  <p>I am trying to create separate frontend/backend services. Frontend only handles UI related processing, while backend will actually do some processing on the file, hence the multipart request forwarding needed.</p>
  <p>The forwarding code on Server A is as follows.
  The solution has been taken from here.
  <a href="https://stackoverflow.com/a/34725635/6569715">https://stackoverflow.com/a/34725635/6569715</a></p>
  <pre class="lang-golang prettyprint-override"><code>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(&quot;%s%s&quot;, 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 := &amp;http.Client{}
      resp, err := client.Do(proxyReq)
      if err != nil {
          return nil, err
      }
      defer resp.Body.Close()
      return resp, nil
  }
  </code></pre>
  <p>And the code on Server B to process the forwarded request:</p>
  <pre class="lang-golang prettyprint-override"><code>func testMultiPart(w http.ResponseWriter, r *http.Request) {
      if err := r.ParseMultipartForm(10 &lt;&lt; 20); err != nil {
          err = errors.Wrap(errors.WithStack(err), &quot;Backend: Failed to parse form&quot;)
          w.WriteHeader(http.StatusInternalServerError)
          fmt.Fprint(w, fmt.Sprintf(&quot;{\&quot;error\&quot;:\&quot;%s\&quot;}&quot;, err.Error())
          return
      }
  }
  </code></pre>
  <p>Any help is appreciated.</p>