glanceapp/glance · error

marshaling body: %v

Error message

marshaling body: %v

What it means

Returned from CustomAPIRequest.initialize when body-type is json and json.Marshal(req.Body) fails. req.Body comes straight from YAML decoding, so this means the configured body contains a value Go's encoding/json cannot serialize.

Source

Thrown at internal/glance/widget-custom-api.go:156

	if req.Body != nil {
		if req.Method == "" {
			req.Method = http.MethodPost
		}

		if req.BodyType == "" {
			req.BodyType = "json"
		}

		if req.BodyType != "json" && req.BodyType != "string" {
			return errors.New("invalid body type, must be either 'json' or 'string'")
		}

		switch req.BodyType {
		case "json":
			encoded, err := json.Marshal(req.Body)
			if err != nil {
				return fmt.Errorf("marshaling body: %v", err)
			}

			req.bodyReader = bytes.NewReader(encoded)
		case "string":
			bodyAsString, ok := req.Body.(string)
			if !ok {
				return errors.New("body must be a string when body-type is 'string'")
			}

			req.bodyReader = strings.NewReader(bodyAsString)
		}

	} else if req.Method == "" {
		req.Method = http.MethodGet
	}

	httpReq, err := http.NewRequest(strings.ToUpper(req.Method), req.URL, req.bodyReader)
	if err != nil {

View on GitHub (pinned to 91324e8de7)

Solutions

  1. If the body is a plain string, switch body-type: string so it is sent verbatim.
  2. Remove .nan/.inf or unserializable values from the YAML body block.
  3. Keep the JSON body to plain mappings, sequences, strings, numbers, booleans.
  4. Check YAML anchors/merge keys (<<) are not producing unexpected nested structures.

Example fix

# before
body-type: json
body: "plain string body"

# after
body-type: string
body: "plain string body"
Defensive patterns

Strategy: validation

Validate before calling

if widget.BodyType == "json" && widget.Body != nil {
    if _, err := json.Marshal(widget.Body); err != nil {
        return fmt.Errorf("body is not JSON-serializable: %w", err)
    }
}

Type guard

// string bodies only make sense with body-type: string
if s, ok := req.Body.(string); ok && req.BodyType == "" {
    req.BodyType = "string" // let plain strings default to string, not json
    _ = s
}

Prevention

When it happens

Trigger: Body value decodes to a type json.Marshal rejects: NaN/Infinity floats (e.g. from YAML .nan/.inf), cyclic structures, channels/funcs, or a map with non-stringable complex keys depending on the YAML decoder's output types.

Common situations: YAML body containing .nan or .inf; exotic YAML anchors producing recursive structures; expecting a plain string body while body-type: json is set.

Related errors


AI-assisted analysis of glanceapp/glance@91324e8de7 (2026-08-15). Data as JSON: /api/errors/328b139089a2047a. Report an issue: GitHub.