You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
465 B
Go
21 lines
465 B
Go
package httputil
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// WriteJSON returns the data as json to the client.
|
|
func WriteJSON(w http.ResponseWriter, data interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
err := json.NewEncoder(w).Encode(data)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// ReadJSON decodes the request body inside the given pointer.
|
|
func ReadJSON(r *http.Request, data interface{}) error {
|
|
return json.NewDecoder(r.Body).Decode(data)
|
|
}
|