2023-09-01 22:27:02 +00:00
|
|
|
// Package response provides convenience functions to write into a http.ResponseWriter.
|
|
|
|
package response
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
2023-10-24 16:55:11 +00:00
|
|
|
|
|
|
|
"github.com/segmentio/encoding/json"
|
2023-09-01 22:27:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
2023-10-24 16:55:11 +00:00
|
|
|
enc := json.NewEncoder(rw)
|
|
|
|
enc.SetSortMapKeys(false)
|
|
|
|
enc.SetAppendNewline(false)
|
|
|
|
|
|
|
|
err := enc.Encode(data)
|
2023-09-01 22:27:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to write JSON response", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON encodes data to rw in YAML format. Returns a pointer to a
|
|
|
|
// HandlerError if encoding fails.
|
|
|
|
func YAML(rw http.ResponseWriter, data interface{}) *httperror.HandlerError {
|
|
|
|
rw.Header().Set("Content-Type", "text/yaml")
|
|
|
|
|
|
|
|
strData, ok := data.(string)
|
|
|
|
if !ok {
|
|
|
|
return httperror.InternalServerError("Unable to write YAML response", errors.New("failed to convert input to string"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(rw, strData)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty merely sets the response code to NoContent (204).
|
|
|
|
func Empty(rw http.ResponseWriter) *httperror.HandlerError {
|
|
|
|
rw.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|