{ "name": "John Doe", "age": 30, "email": "[email protected]" }
type User struct { Name string `json:"name"` Age int `json:"age"` Email string `json:"email"` } func createUser(w rest.ResponseWriter, r *rest.Request) { user := User{} err := r.DecodeJsonPayload(&user) if err != nil { rest.Error(w, err.Error(), http.StatusInternalServerError) return } // Do something with the user struct... }
{ "username": "johndoe", "password": "secret" }
type LoginCredentials struct { Username string `json:"username"` Password string `json:"password"` } func login(w rest.ResponseWriter, r *rest.Request) { creds := LoginCredentials{} err := r.DecodeJsonPayload(&creds) if err != nil { rest.Error(w, err.Error(), http.StatusInternalServerError) return } // Do something with the credentials struct... }