I have a route which works with multipart/form-data. All was good before I tried to pass nested objects via postman. There are a lot of code in this function but I removed it for this question because it does not matter.
func (h *handler) SubmitFormNewPark(w http.ResponseWriter, r *http.Request) {
ctx := context.InitFromHTTP(w, r, "submit_form_new_park")
defer ctx.OnExit(nil)
err := r.ParseMultipartForm(0)
if err != nil {
logger.Get().Warn(ctx, "can't parse new park form", zap.Error(err))
h.writeResponse(ctx, w, models.FormParkResponse{Success: false})
return
}
multipartForm := make(map[string]interface{})
for key, value := range r.MultipartForm.Value {
multipartForm[key] = value
}
}
Firstly all was good because I had not nested object
But after I added nested object there are different keys in r.MultipartForm.Value
If I print the r.MultipartForm.Value I get next map
map[city:[city] entity_name:[entity_name] ip_info[ip_address]:[ip_address] ip_info[ip_fio]:[ip_fio] name:[Name] office_address:[office address] office_phone:[74954954949]]
But I expect get "ip_info" key as map with keys ip_address and ip_fio
I also tried to use keys such as ip_info[0][ip_fio] and ip_info[0][ip_address].




req.GetParameters("ip_info[ip_fio]"). What I did was send multiple values on the same key and GetParameters will return a string slice with the values from that key.multipart/form-data. There are several (incompatible) ways of marshaling complex/nested structures into this type of data. I'm not sure which format postman uses (and maybe it supports multiple ones), but you'll need to find a Go library that's compatible with that format. Or if it's an option, maybe use JSON, or some other more flexible data type instead.