I have a defined struct in my little web server go program, then Im trying to call a POST method, from another end point, with unspecified number of struct pieces, I defined in my go web server program. Im creating a slice of the struct, but how do I unmarshall-decode the incoming json onto the struct? How do I define a variable size?
Started with this just limiting the size of the incoming json
func (app *application) readJSON(w http.ResponseWriter, r http.Request, data interface{}) error{
maxBytes := 10241024 // one megabyte
r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytes))
dec := json.NewDecoder((r.Body))
dec.DisallowUnknownFields()
err := dec.Decode(data)
if err != nil {
return err
}
err = dec.Decode(&struct{}{})
if err != io.EOF {
return errors.New("body must only contain a single JSON value")
}
return nil
}