title: Handle file uploading with go
body: <p>I've started playing with go very recently so I'm still a noob, sorry if I make too many mistakes. I've been trying to fix this for a long time but I just don't understand what's going on. In my main.go file I have a main function:</p>
  
  <pre><code>func main() {
      http.HandleFunc("/", handler)
      http.HandleFunc("/submit/", submit)
      log.Fatal(http.ListenAndServe(":8080", nil))
  }
  </code></pre>
  
  <p>The handler function looks like this:</p>
  
  <pre><code>func handler(w http.ResponseWriter, r *http.Request) {
      data, _ := ioutil.ReadFile("web/index.html")
      w.Write(data)
  }
  </code></pre>
  
  <p>I know this is not the best way to serve a website
  The submit function looks like this:</p>
  
  <pre><code>func submit(w http.ResponseWriter, r *http.Request) {
      log.Println("METHOD IS " + r.Method + " AND CONTENT-TYPE IS " + r.Header.Get("Content-Type"))
      r.ParseMultipartForm(32 &lt;&lt; 20)
      file, header, err := r.FormFile("uploadFile")
      if err != nil {
          json.NewEncoder(w).Encode(Response{err.Error(), true})
          return
      }
      defer file.Close()
  
      out, err := os.Create("/tmp/file_" + time.Now().String() + ".png")
      if err != nil {
          json.NewEncoder(w).Encode(Response{err.Error(), true})
          return
      }
      defer out.Close()
  
      _, err = io.Copy(out, file)
      if err != nil {
          json.NewEncoder(w).Encode(Response{err.Error(), true})
          return
      }
  
      json.NewEncoder(w).Encode(Response{"File '" + header.Filename + "' submited successfully", false})
  }
  </code></pre>
  
  <p>The problem is when the submit function is executed, <code>r.Method</code> is <code>GET</code> and <code>r.Header.Get("Content-Type")</code> is an empty string, then it continues until the first if where r.FormFile returns the following error:
  <code>request Content-Type isn't multipart/form-data</code>
  I don't understand why r.Method is always GET and there's no Content-Type. I've tried to do the index.html in many different ways but r.Method is always GET and Content-Type is empty. Here's the function in index.html that uploads a file:</p>
  
  <pre><code>function upload() {
      var formData = new FormData();
      formData.append('uploadFile', document.querySelector('#file-input').files[0]);
      fetch('/submit', {
          method: 'post',
          headers: {
            "Content-Type": "multipart/form-data"
          },
          body: formData
      }).then(function json(response) {
          return response.json()
      }).then(function(data) {
          window.console.log('Request succeeded with JSON response', data);
      }).catch(function(error) {
          window.console.log('Request failed', error);
      });
  }
  </code></pre>
  
  <p>And here's the HTML:</p>
  
  <pre><code>&lt;input id="file-input" type="file" name="uploadFile" /&gt;
  </code></pre>
  
  <p>Note that the  tag is not inside a  tag, I thought that could be the problem so I changed both the function and the HTML to something like this:</p>
  
  <pre><code>function upload() {
      fetch('/submit', {
          method: 'post',
          headers: {
            "Content-Type": "multipart/form-data"
          },
          body: new FormData(document.querySelector('#form')
      }).then(function json(response) {
          return response.json()
      }).then(function(data) {
          window.console.log('Request succeeded with JSON response', data);
      }).catch(function(error) {
          window.console.log('Request failed', error);
      });
  }
  
  &lt;form id="form" method="post" enctype="multipart/form-data" action="/submit"&gt;&lt;input id="file-input" type="file" name="uploadFile" /&gt;&lt;/form&gt;
  </code></pre>
  
  <p>But that didn't work neither. I've searched with Google how to use fetch() and how to receive a file upload from go and I've seen that they are pretty similar to mine, I don't know what I'm doing wrong.</p>
  
  <p><strong>UPDATE:</strong>
  After using <code>curl -v -F 'uploadFile=@\"C:/Users/raul-/Desktop/test.png\"' http://localhost:8080/submit</code> I get the following output:</p>
  
  <pre><code>* Trying ::1...
  * Connected to localhost (::1) port 8080 (#0)
  &gt; POST /submit HTTP/1.1
  &gt; Host: localhost:8080
  &gt; User-Agent: curl/7.45.0
  &gt; Accept: */*
  &gt; Content-Length: 522
  &gt; Expect: 100-continue
  &gt; Content-Type: multipart/form-data; boundary=---------------------------a17d4e54fcec53f8
  &gt;
  &lt; HTTP/1.1 301 Moved Permanently
  &lt; Location: /submit/
  &lt; Date: Wed, 18 Nov 2015 14:48:38 GMT
  &lt; Content-Length: 0
  &lt; Content-Type: text/plain; charset=utf-8
  * HTTP error before end of send, stop sending
  &lt;
  * Closing connection 0
  </code></pre>
  
  <p>The console where I'm running <code>go run main.go</code> outputs nothing when using curl.</p>