MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / writeJSONStatus

Method writeJSONStatus

rest/handler.go:1543–1563  ·  view source on GitHub ↗

Writes an object to the response in JSON format. If status is nonzero, the header will be written with that status.

(status int, value interface{})

Source from the content-addressed store, hash-verified

1541// Writes an object to the response in JSON format.
1542// If status is nonzero, the header will be written with that status.
1543func (h *handler) writeJSONStatus(status int, value interface{}) {
1544 if !h.requestAccepts("application/json") {
1545 base.WarnfCtx(h.ctx(), "Client won't accept JSON, only %s", h.rq.Header.Get("Accept"))
1546 h.writeStatus(http.StatusNotAcceptable, "only application/json available")
1547 return
1548 }
1549
1550 jsonOut, err := base.JSONMarshalCanonical(value)
1551 if err != nil {
1552 base.WarnfCtx(h.ctx(), "Couldn't serialize JSON for %v : %s", base.UD(value), err)
1553 h.writeStatus(http.StatusInternalServerError, "JSON serialization failed")
1554 return
1555 }
1556 if base.ValDefault(h.server.Config.API.Pretty, false) {
1557 var buffer bytes.Buffer
1558 _ = json.Indent(&buffer, jsonOut, "", " ")
1559 jsonOut = append(buffer.Bytes(), '\n')
1560 }
1561
1562 h.writeRawJSONWithoutClientVerification(status, jsonOut)
1563}
1564
1565// writeRawJSON writes the given bytes as a JSON response with a 200 OK status.
1566func (h *handler) writeRawJSON(b []byte) {

Callers 3

writeJSONMethod · 0.95
handleGetAttachmentMethod · 0.95
handleBulkDocsMethod · 0.95

Calls 9

requestAcceptsMethod · 0.95
ctxMethod · 0.95
writeStatusMethod · 0.95
WarnfCtxFunction · 0.92
JSONMarshalCanonicalFunction · 0.92
UDFunction · 0.92
ValDefaultFunction · 0.92
GetMethod · 0.45

Tested by

no test coverage detected