MCPcopy Create free account
hub / github.com/tailscale/tailscale / handleJSON

Function handleJSON

client/web/web.go:534–552  ·  view source on GitHub ↗

handleJSON manages decoding the request's body JSON as data and passing it on to the provided handler function.

(h func(ctx context.Context, data data) error)

Source from the content-addressed store, hash-verified

532// handleJSON manages decoding the request's body JSON as data and passing it
533// on to the provided handler function.
534func handleJSON[data any](h func(ctx context.Context, data data) error) http.HandlerFunc {
535 return func(w http.ResponseWriter, r *http.Request) {
536 defer r.Body.Close()
537 var body data
538 if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
539 http.Error(w, err.Error(), http.StatusInternalServerError)
540 return
541 }
542 if err := h(r.Context(), body); err != nil {
543 if httpErr, ok := errors.AsType[tsweb.HTTPError](err); ok {
544 tsweb.WriteHTTPError(w, r, httpErr)
545 } else {
546 http.Error(w, err.Error(), http.StatusInternalServerError)
547 }
548 return
549 }
550 w.WriteHeader(http.StatusOK)
551 }
552}
553
554var contextKeyPeer = ctxkey.New("peer-capabilities", peerCapabilities{})
555

Callers

nothing calls this directly

Calls 6

WriteHTTPErrorFunction · 0.92
CloseMethod · 0.65
ErrorMethod · 0.65
ContextMethod · 0.65
DecodeMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…