httputils_write_json.go 436 B

123456789101112131415
  1. package httputils // import "github.com/docker/docker/api/server/httputils"
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. )
  6. // WriteJSON writes the value v to the http response stream as json with standard json encoding.
  7. func WriteJSON(w http.ResponseWriter, code int, v interface{}) error {
  8. w.Header().Set("Content-Type", "application/json")
  9. w.WriteHeader(code)
  10. enc := json.NewEncoder(w)
  11. enc.SetEscapeHTML(false)
  12. return enc.Encode(v)
  13. }