Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
Having trouble uploading files from Javascript and receiving it through Go

I set up a simple frontend service in JavaScript using Axios. This service takes a list of files and sends to my server code using a post request. My JavaScript looks like this

const testPost = async (files) => {
  let formData = new FormData();

  files.forEach((file) => {
    formData.append("file", file);
  });

  const res = await axios({
    method: "POST",
    url: baseUrl,
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });

  return res;
};

And my Go code looks like this:

func (h *SendHandler) TestPost(w http.ResponseWriter, r *http.Request) {
    //Allow CORS here By * or specific origin
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

    formdata := r.MultipartForm
    fmt.Println(formdata)
}

I think my problem lies in the body of my TestPost function in Go because I click send in my frontend code, I receive a response. In my Go function, when I print my formdata, it outputs <nil>. I want to be able to access each file in the list but I can't even access the formdata. I'm still new to both web dev and Go so there is definitely something I am misunderstanding here. Any pointers?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The documentation for MultipartForm says:

MultipartForm is the parsed multipart form, including file uploads. This field is only available after ParseMultipartForm is called.

Fix by calling ParseMultipartForm before using r.MultipartForm.

err := r.ParseMultipartForm(maxMemory)
if err != nil {
    http.Error(w, "Bad Request", http.StatusBadRequest)
    return
}
// r.MultipartForm is now set.
fmt.Println(r.MultipartForm)  // does not print nil
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda