filebrowser/tools/server/response.go

25 lines
460 B
Go
Raw Normal View History

2016-03-06 20:37:49 +00:00
package server
import (
"encoding/json"
"net/http"
)
2016-03-12 09:25:15 +00:00
// RespondJSON used to send JSON responses to the web server
2016-03-12 13:03:31 +00:00
func RespondJSON(w http.ResponseWriter, message interface{}, code int, err error) (int, error) {
2016-03-12 09:25:15 +00:00
if message == nil {
message = map[string]string{}
}
2016-03-06 20:37:49 +00:00
msg, msgErr := json.Marshal(message)
if msgErr != nil {
return 500, msgErr
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(msg)
return 0, err
}