Reputation: 11532
How to parse parameters from POST
body to map[string] string
?
I am using gin and I can parse to predefined structure but in this case I don't know key names. ( I can when I know key names but when I don't know is a problem )
type Body struct {
Name string
Email string
}
body := Body{}
err := json.NewDecoder( c.Request.Body ).Decode( &body )
if err != nil {
c.String( http.StatusServiceUnavailable, err.Error() )
return
}
The body looks like this:
{
"param1": "1",
"param2": "1",
"param3": "1",
"param4": "1"
}
Upvotes: 0
Views: 4604
Reputation:
Try this:
package main
import (
"fmt"
"encoding/json"
)
func main() {
j := `{"foo": "aa", "baz": "bb", "qux": "cc"}`
byt := []byte(j)
var dat map[string]string
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fmt.Println(dat)
}
Output:
map[qux:cc foo:aa baz:bb]
Upvotes: 4