cayleygraph/cayley · error

cannot remove nil value

Error message

cannot remove nil value

What it means

After unmarshaling the DELETE body, ServeNodeDelete checks whether the decoded value is nil. An empty body or the JSON literal `null` decodes to a nil Go value, and a removal cannot be performed against a nil value, so the API returns 400 'cannot remove nil value'.

Source

Thrown at server/http/api_v2.go:332

		jsonResponse(w, http.StatusBadRequest, fmt.Errorf("format is not supported for reading nodes"))
		return
	}
	const limit = 128*1024 + 1
	rd := io.LimitReader(r.Body, limit)
	data, err := ioutil.ReadAll(rd)
	if err != nil {
		jsonResponse(w, http.StatusBadRequest, err)
		return
	} else if len(data) == limit {
		jsonResponse(w, http.StatusBadRequest, fmt.Errorf("request data is too large"))
		return
	}
	v, err := format.UnmarshalValue(data)
	if err != nil {
		jsonResponse(w, http.StatusBadRequest, err)
		return
	} else if v == nil {
		jsonResponse(w, http.StatusBadRequest, fmt.Errorf("cannot remove nil value"))
		return
	}
	h, err := api.handleForRequest(r)
	if err != nil {
		jsonResponse(w, http.StatusBadRequest, err)
		return
	}
	err = h.RemoveNode(v)
	if err != nil {
		jsonResponse(w, http.StatusInternalServerError, err)
		return
	}
	w.Header().Set(hdrContentType, contentTypeJSON)
	const n = 1
	fmt.Fprintf(w, `{"result": "Successfully deleted %d nodes.", "count": %d}`+"\n", n, n)
}

type checkWriter struct {

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Send a concrete JSON value, e.g. {"id":"<node-id>"}, describing what to remove.
  2. Fix client code so the node identifier is populated and non-null before the request is sent.
  3. Verify the Content-Type matches the payload format so the body parses into the intended value.

Example fix

// before
curl -X POST -H 'Content-Type: application/json' -d 'null' http://localhost:64210/api/v2/node/delete
// after
curl -X POST -H 'Content-Type: application/json' -d '{"id":"alice"}' http://localhost:64210/api/v2/node/delete
Defensive patterns

Strategy: validation

Validate before calling

if len(body) == 0 || string(body) == "null" || payload == nil {
    return errors.New("node delete requires a non-null JSON value")
}

Prevention

When it happens

Trigger: Sending an empty body or the literal `null` to /api/v2/node/delete so format.UnmarshalValue returns a nil interface.

Common situations: curl calls with -d '' or -d 'null'; HTTP clients serializing undefined/None variables as null; test harnesses invoking the endpoint without a payload.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/f3f7a085f8878866. Report an issue: GitHub.