portainer/api/http/response/response.go

26 lines
698 B
Go

package response
import (
"encoding/json"
"net/http"
httperror "github.com/portainer/portainer/http/error"
)
// JSON encodes data to rw in JSON format. Returns a pointer to a
// HandlerError if encoding fails.
func JSON(rw http.ResponseWriter, data interface{}) *httperror.HandlerError {
rw.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(rw).Encode(data)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to write JSON response", err}
}
return nil
}
// Empty merely sets the response code to NoContent (204).
func Empty(rw http.ResponseWriter) *httperror.HandlerError {
rw.WriteHeader(http.StatusNoContent)
return nil
}