I have a form look like this
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="hidden" name="metadata[mimetype]" value="text/plain"/>
<input type="hidden" name="metadata[size]" value="1024" />
<input type="hidden" name="metadata[type]" value="file" />
<input type="file" name="file" multiple/>
<input type="submit"/>
</form>
In my Go app I want to get a map look like this
["mimetype":"text/plain",...]
but I am getting metadata["mimetype"]
as key
Here is a my logic in Go
for key, values := range rq.Form {
if len(values) > 0 {
value := values[0]
fmt.Println(key, value)
}
}
Why not simply change the form input names to remove the metadata[]
part?
If this can't be done for some reason (e.g. client side Javascript relies on these names) then you could create a new map with something like the below:
values := make(map[string]string)
for k, v := range r.Form {
if len(v) > 0 {
k = strings.TrimPrefix(k, "metadata[")
k = strings.TrimSuffix(k, "]")
values[k] = v[0]
}
}
Note this is assuming there is only one input with each name (string
as opposed to []string
) as this is the format you indicated your map should be in.