Hello, this is my first post here on this forum.
I have a problem in parsing multipart/form-data.
Here is what I did:
func handleIncomingUploads(w http.ResponseWriter, r *http.Request){
r.ParseMultipartForm(math.MaxInt64)
log.Printf("%#v", r.Header.Get("Content-Type"))
username:=r.FormValue("username")
imei:=r.PostFormValue("imei")
log.Println(username)
log.Println(imei)
file, handler, err:=r.FormFile("file")
if err!=nil{
log.Println("Error retrieving the file:", err)
return
}
defer file.Close()
log.Printf("Uploaded File: %+v\n", handler.Filename)
log.Printf("File Size: %+v\n", handler.Size)
log.Printf("MIME Header: %+v\n", handler.Header)
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
log.Println(err)
}
filepath:="boom-"+username
err=ioutil.WriteFile(filepath, fileBytes, 0777)
if err != nil {
log.Println(err)
}
log.Println("File successfully uploaded.")
}
What I’m doing here is trying to parse a form having three fields: username
, imei
and file
.
The last one contains a file (obviously) and it is well parsed and saved by this code.
Now the problem is on the other two, they are two string values, but they are not correctly parsed, i have an empty string.
I made some attempts using Postman and I noticed that those two fields are correctly parsed only if I choose x-www-form-urlencoded
instead of form-data
.
So I can’t understand what’s wrong here. Can someone help me please?
Thanks in advance.