{"record":{"id":"5c416057d897793c","repo":"googleapis/mcp-toolbox","slug":"failed-to-unmarshal-response-v","errorCode":null,"errorMessage":"failed to unmarshal response: %v","messagePattern":"failed to unmarshal response: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/dgraph/dgraph.go","lineNumber":327,"sourceCode":"\t\t\t!strings.Contains(err.Error(), \"unable to authenticate the refresh token\") {\n\t\t\treturn hc.loginWithToken()\n\t\t}\n\t\treturn err\n\t}\n\n\tif err := checkError(resp); err != nil {\n\t\treturn err\n\t}\n\n\tvar r struct {\n\t\tData struct {\n\t\t\tAccessJWT  string `json:\"accessJWT\"`\n\t\t\tRefreshJWT string `json:\"refreshJWT\"`\n\t\t} `json:\"data\"`\n\t}\n\n\tif err := json.Unmarshal(resp, &r); err != nil {\n\t\treturn fmt.Errorf(\"failed to unmarshal response: %v\", err)\n\t}\n\n\tif r.Data.AccessJWT == \"\" {\n\t\treturn fmt.Errorf(\"no access JWT found in the response\")\n\t}\n\tif r.Data.RefreshJWT == \"\" {\n\t\treturn fmt.Errorf(\"no refresh JWT found in the response\")\n\t}\n\n\thc.AccessJwt = r.Data.AccessJWT\n\thc.RefreshToken = r.Data.RefreshJWT\n\treturn nil\n}\n\nfunc (hc *DgraphClient) healthCheck() error {\n\turl, err := getUrl(hc.baseUrl, \"/health\", nil)\n\tif err != nil {\n\t\treturn err","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/dgraph/dgraph.go#L309-L345","documentation":"doLogin fails to parse the body returned by the Dgraph /login endpoint as JSON into the expected {data:{accessJWT,refreshJWT}} shape. This means the server (or an intermediary) responded with malformed JSON, an empty body, or non-JSON content such as an HTML error page. The underlying parse error is wrapped with %v.","triggerScenarios":"json.Unmarshal(resp, &r) errors after the login POST completes — response body is empty, truncated, HTML (proxy/load-balancer error page), or JSON with a different structure than expected.","commonSituations":"Wrong port (hitting Dgraph Alpha's gRPC port 9080 over HTTP), a reverse proxy or API gateway returning an HTML 502/403 page, TLS termination issues, or a Dgraph version whose login response shape differs.","solutions":["Verify hc.baseUrl points to Dgraph Alpha's HTTP port (default 8080), not the gRPC port 9080.","Log or print the raw response body to see whether it is HTML, empty, or JSON with an unexpected shape.","Bypass intermediaries: curl the /login endpoint directly to rule out proxy/load-balancer error pages.","Confirm the Dgraph version's /login response uses {\"data\":{\"accessJWT\":...,\"refreshJWT\":...}} and update the parsing struct if not.","Check TLS configuration if connecting through HTTPS — certificate errors can yield non-JSON responses."],"exampleFix":"// debugging: inspect what /login actually returned\nfmt.Printf(\"login response: %s\\n\", string(resp))\n// before\n// (no diagnostics — opaque unmarshal failure)\n// after\n// raw body is logged before unmarshal, revealing HTML proxy pages or empty bodies","handlingStrategy":"try-catch","validationCode":"// preflight: ensure the endpoint speaks JSON before logging in\nresp, err := http.Get(baseURL + \"/health\")\nif err != nil || resp.StatusCode != 200 {\n  return fmt.Errorf(\"dgraph endpoint not reachable/healthy at %s\", baseURL)\n}","typeGuard":"func isJSONResponse(body []byte) bool {\n  t := bytes.TrimSpace(body)\n  return len(t) > 0 && (t[0] == '{' || t[0] == '[')\n}","tryCatchPattern":"if err := doLogin(...); err != nil {\n  if strings.Contains(err.Error(), \"failed to unmarshal response\") {\n    log.Printf(\"non-JSON login response; verify baseURL port (8080 vs 9080) and proxies: %v\", err)\n    return\n  }\n}","preventionTips":["Point baseURL at Dgraph Alpha's HTTP port (default 8080), never the gRPC port 9080.","Log the raw login response body when debugging to spot HTML proxy error pages.","Run a /health preflight check before the first login call.","Bypass or configure load balancers so they do not serve HTML error pages to API clients.","Pin and test against the Dgraph version whose login response schema you parse."],"tags":["json","http-response","dgraph","login","unmarshal"],"backgroundTag":"invalid-json-response","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}