How can I receive an uploaded file using a Golang net/http server?
<p>I'm playing around with <a href="https://github.com/gorilla/mux" rel="noreferrer">Mux</a> and <code>net/http</code>. Lately, I'm trying to get a simple server with one endpoint to accept a file upload.</p>

<p>Here's the code I've got so far:</p>

<p><strong>server.go</strong></p>

<pre><code>package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    router := mux.NewRouter()
    router.
        Path("/upload").
        Methods("POST").
        HandlerFunc(UploadCsv)
    fmt.Println("Starting")
    log.Fatal(http.ListenAndServe(":8080", router))
}
</code></pre>

<p><strong>endpoint.go</strong></p>

<pre><code>package main

import (
    "fmt"
    "net/http"
)

func UploadFile(w http.ResponseWriter, r *http.Request) {
    err := r.ParseMultipartForm(5 * 1024 * 1024)
    if err != nil {
        panic(err)
    }

    fmt.Println(r.FormValue("fileupload"))
}
</code></pre>

<p>I think I've narrowed the issue down to actually retrieving the body from the request inside <code>UploadFile</code>. When I run this cURL command:</p>

<pre><code>curl http://localhost:8080/upload -F "fileupload=@test.txt" -vvv
</code></pre>

<p>I get an empty response (as expected; I'm not printing to the <code>ResponseWriter</code>), but I just get a new (empty) line printed at the prompt where I'm running the server, instead of the request body.</p>

<p>I'm sending the file as multipart (AFAIK, implied by using <code>-F</code> rather than <code>-d</code> in cURL), and cURL's verbose output is showing 502 bytes sent:</p>

<pre><code>$ curl http://localhost:8080/upload -F "fileupload=@test.txt" -vvv
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
&gt; POST /upload HTTP/1.1
&gt; Host: localhost:8080
&gt; User-Agent: curl/7.51.0
&gt; Accept: */*
&gt; Content-Length: 520
&gt; Expect: 100-continue
&gt; Content-Type: multipart/form-data; boundary=------------------------b578878d86779dc5
&gt; 
&lt; HTTP/1.1 100 Continue
&lt; HTTP/1.1 200 OK
&lt; Date: Fri, 18 Nov 2016 19:01:50 GMT
&lt; Content-Length: 0
&lt; Content-Type: text/plain; charset=utf-8
&lt; 
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
</code></pre>

<p>What's the proper way to receive files uploaded as multipart form data using a <code>net/http</code> server in Go?</p>