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
- If the body is a plain string, switch body-type: string so it is sent verbatim.
- Remove .nan/.inf or unserializable values from the YAML body block.
- Keep the JSON body to plain mappings, sequences, strings, numbers, booleans.
- 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
- Match body-type to the body's actual shape: mapping/list → json, scalar text → string.
- Avoid .nan/.inf and exotic YAML constructs in request bodies.
- Test the request with curl -d @body.json before putting it in config.
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
- initializing primary request: %v
- initializing subrequest %q: %v
- parsing template: %w
- invalid body type, must be either 'json' or 'string'
- body must be a string when body-type is 'string'
AI-assisted analysis of glanceapp/glance@91324e8de7 (2026-08-15).
Data as JSON: /api/errors/328b139089a2047a.
Report an issue: GitHub.