I have an API that is expecting a "username":"string" request body, I am unmarshalling the request body into a user struct that matches what I am expecting. My code does not throw an error on mismatched struct data. Is there a function I could use instead of json.Unmarshal? If not how would I be able to catch a 400 error in this case? (I have also tried using json.decoder)
Here is my code:
type User struct {
Username string `json:"username" gorm:"primary_key;Column:username"`
}
var user models.User
body, err := ioutil.ReadAll(r.Body)
if err = json.Unmarshal(body, &user); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}