// create a response object response := &restful.Response{} // set the status code of the response response.WriteHeader(http.StatusOK) // create an entity to return as JSON person := struct { Name string `json:"name"` Age int `json:"age"` }{ Name: "John Smith", Age: 30, } // write the header and the entity to the response writer response.WriteHeaderAndEntity(http.StatusOK, person)
// create a response object response := &restful.Response{} // set a custom header on the response response.Header().Set("X-Custom-Header", "value") // create an error response errResponse := struct { Message string `json:"message"` }{ Message: "An error occurred", } // write the header and the error response to the response writer response.WriteHeaderAndEntity(http.StatusBadRequest, errResponse)In this example, we create a RESTful response object and set a custom header on the response. We then create an error response and use the WriteHeaderAndEntity method to write both the header and the error response to the response writer. Overall, the WriteHeaderAndEntity method is a convenient way to write both the HTTP header and the JSON entity to the response writer in a single call. It is part of the go github.com.emicklei.go-restful package.