Search code examples
gogorillamux

Go r.PostForm is empty


I tried to send form data and file to server but from the server the form value is empty , but i receive file only

SERVER

func CreatePost(w http.ResponseWriter, r *http.Request) {
    createdAt := time.Now().String()
    r.ParseForm()
    data := r.PostForm.Get("body")
    fmt.Println(r.PostForm)
    r.ParseMultipartForm(10 << 20)
    file, header, err := r.FormFile("file")
    ///Some Codes///////
}

TERMINAL OUTPUT

19:33:08 app         | listerning on 8080
19:33:11 app         | map[]

CLICK ME


Solution

  • According to the docs for ParseForm:

    For other HTTP methods, or when the Content-Type is not application/x-www-form-urlencoded, the request Body is not read, and r.PostForm is initialized to a non-nil, empty value.

    When you submit a file, the content type will be one of the multipart types.

    Based on these docs, I'd try this:

     r.ParseMultipartForm(10<<20)
     data := r.PostForm.Get("body")
     fmt.Println(r.PostForm)
     file, header, err := r.FormFile("file")