MCPcopy
hub / github.com/go-kit/kit / EncodeJSONResponse

Function EncodeJSONResponse

transport/http/server.go:163–181  ·  view source on GitHub ↗

EncodeJSONResponse is a EncodeResponseFunc that serializes the response as a JSON object to the ResponseWriter. Many JSON-over-HTTP services can use it as a sensible default. If the response implements Headerer, the provided headers will be applied to the response. If the response implements StatusC

(_ context.Context, w http.ResponseWriter, response interface{})

Source from the content-addressed store, hash-verified

161// will be applied to the response. If the response implements StatusCoder, the
162// provided StatusCode will be used instead of 200.
163func EncodeJSONResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
164 w.Header().Set("Content-Type", "application/json; charset=utf-8")
165 if headerer, ok := response.(Headerer); ok {
166 for k, values := range headerer.Headers() {
167 for _, v := range values {
168 w.Header().Add(k, v)
169 }
170 }
171 }
172 code := http.StatusOK
173 if sc, ok := response.(StatusCoder); ok {
174 code = sc.StatusCode()
175 }
176 w.WriteHeader(code)
177 if code == http.StatusNoContent {
178 return nil
179 }
180 return json.NewEncoder(w).Encode(response)
181}
182
183// DefaultErrorEncoder writes the error to the ResponseWriter, by default a
184// content type of text/plain, a body of the plain text of the error, and a

Callers

nothing calls this directly

Calls 5

SetMethod · 0.65
HeadersMethod · 0.65
AddMethod · 0.65
StatusCodeMethod · 0.65
WriteHeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…