将图像作为POST请求中的主体附加到golang服务器

I have a local golang server with an endpoint that listens to POST requests, decodes the body of the request, and persists it. This works when I manually curl the endpoint like

    curl -X POST localhost:8080/newimage --data-binary "PATH"

However, I'm having trouble successfully uploading a file in a POST request through a gui I'm working on. I'm using https://github.com/okonet/react-dropzone to drop a File and appending it to a FormData object, but the golang server does not seem to be receiving a populated body.

This is how I'm creating the AJAX query:

    formData = new FormData();
    formData.append("image", file)
    $.ajax({
        url: "http://localhost:8080/items/81d648b0-25f9-434e-9129-fe52575865dd/newimage",
        type: "POST",
        data: formData,
        processData: false
    }).done(function(res) {
        console.log(res);
    });

And the backend server:

func (h *ItemHandler) PostImage(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
itemID := vars["id"]
assetID := newAssetID()

// verify image
img, _, err := image.Decode(req.Body)
if err != nil {
    log.Printf("could not decode body into an image")
    resp.Header().Add("Access-Control-Allow-Origin", "*")
    resp.WriteHeader(http.StatusBadRequest)
    resp.Write([]byte("could not decode body image"))
    return
}

Would appreciate any suggestions.

FormData is very different from sending the image as raw bytes. Here is an example on how to get the files from a multipart/form-data request:

func (h *ItemHandler) PostImage(resp http.ResponseWriter, req *http.Request) {
  vars := mux.Vars(req)
  itemID := vars["id"]
  assetID := newAssetID()

  file, _, err := req.FormFile("image")
  if err != nil{
    log.Print(err)
    resp.WriteHeader(http.StatusBadRequest)
    return
  }

  // verify image
  img, _, err := image.Decode(file)
  if err != nil {
    log.Printf("could not decode body into an image")
    resp.Header().Add("Access-Control-Allow-Origin", "*")
    resp.WriteHeader(http.StatusBadRequest)
    resp.Write([]byte("could not decode body image"))
    return
  }
  ...
}

See https://golang.org/pkg/net/http/#Request.FormFile