import ( "github.com/ant0ine/go-json-rest/rest" "net/http" ) func getUser(w rest.ResponseWriter, r *rest.Request) { userID := r.PathParam("userID") //retrieve userID from URL username := r.URL.Query().Get("username") //retrieve username from URL query string rest.Error(w, "User not found", http.StatusNotFound) //return an error if user not found }
import ( "encoding/json" "github.com/ant0ine/go-json-rest/rest" "net/http" ) type User struct { ID string `json:"id"` Name string `json:"name"` Email string `json:"email"` } func createUser(w rest.ResponseWriter, r *rest.Request) { //parse incoming JSON request user := &User{} err := r.DecodeJsonPayload(user) if err != nil { rest.Error(w, "Error parsing JSON", http.StatusBadRequest) return } //save user to database here w.WriteHeader(http.StatusCreated) }In this example, the createUser function uses the DecodeJsonPayload method of the rest.Request struct to parse the incoming JSON request body into a User struct. If there is an error parsing the JSON, an error message is returned. Otherwise, the code can handle saving the user to a database and return an HTTP status created response.