{"record":{"id":"7e630998ae13b2c7","repo":"grafana/k6","slug":"decoding-response-w","errorCode":null,"errorMessage":"decoding response: %w","messagePattern":"decoding response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloudapi/provisioning/http_client.go","lineNumber":73,"sourceCode":"\treq.Header.Set(\"User-Agent\", \"k6cloud/\"+p.version)\n\n\tresp, err := doWithRetry(p.httpClient, req)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tdefer func() {\n\t\t_, _ = io.Copy(io.Discard, resp.Body)\n\t\t_ = resp.Body.Close()\n\t}()\n\n\tif err := CheckResponse(resp); err != nil {\n\t\treturn err\n\t}\n\n\tif v != nil {\n\t\tif err := json.NewDecoder(resp.Body).Decode(v); err != nil && !errors.Is(err, io.EOF) {\n\t\t\treturn fmt.Errorf(\"decoding response: %w\", err)\n\t\t}\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":55,"sourceCodeEnd":79,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/cloudapi/provisioning/http_client.go#L55-L79","documentation":"Returned by HTTPClient.Do (internal/cloudapi/provisioning/http_client.go:73) when the 2xx response body cannot be JSON-decoded into the caller's target v (EOF is tolerated, so empty 2xx bodies are fine). It fires after CheckResponse has already validated the status, so the server answered 'OK' but its body is not the JSON the client expected: malformed JSON, truncated body, or a shape mismatch (json.UnmarshalTypeError, e.g. a string where a number is declared).","triggerScenarios":"Metrics-push or notify endpoints answering 2xx with non-JSON (HTML error page injected by a proxy), truncated responses from connection resets mid-body, or schema drift where the API's JSON field types no longer match the Go decode target.","commonSituations":"Corporate proxies/WAFs replacing responses; API version skew between the k6 client and a newer/older cloud backend; structs hand-maintained in forks drifting from the real payload; connection instability truncating chunked bodies.","solutions":["Classify via the wrapped error: *json.SyntaxError → body isn't valid JSON (capture it); *json.UnmarshalTypeError → your struct's types don't match the payload.","Log or dump the raw response body on failure (temporarily wrap resp.Body with a TeeReader-style capture) to see whether it is HTML/proxy output or truncated JSON.","For proxy interference, bypass it for the cloud host (NO_PROXY) or fix TLS/interception config.","For type mismatches, align the decode target with the current cloud API schema (update k6 / the SDK types).","Treat truncation-from-network as transient: retry the push."],"exampleFix":"// before\nvar result v6.Response\nif err := httpClient.Do(req, &result); err != nil {\n\treturn err // \"decoding response: invalid character '<' ...\"\n}\n\n// after — distinguish the two decode failure classes\nif err := httpClient.Do(req, &result); err != nil {\n\tvar typeErr *json.UnmarshalTypeError\n\tswitch {\n\tcase errors.As(err, &typeErr):\n\t\treturn fmt.Errorf(\"response field %q has type %s, expected %s — schema mismatch: %w\", typeErr.Field, typeErr.Value, typeErr.Type.String(), err)\n\tcase strings.Contains(err.Error(), \"invalid character\"):\n\t\treturn fmt.Errorf(\"response is not JSON (proxy or error page?) — inspect body: %w\", err)\n\t}\n\treturn err\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := p.Do(req, &out); err != nil {\n\tvar typeErr *json.UnmarshalTypeError\n\tvar synErr *json.SyntaxError\n\tswitch {\n\tcase errors.As(err, &typeErr):\n\t\t// schema drift — update decode target; not transient\n\t\treturn fmt.Errorf(\"response schema mismatch at field %q: %w\", typeErr.Field, err)\n\tcase errors.As(err, &synErr):\n\t\t// body not JSON (proxy/error page) or truncated — capture and re-raise with context\n\t\treturn fmt.Errorf(\"malformed JSON response (offset %d): %w\", synErr.Offset, err)\n\t}\n\treturn err\n}","preventionTips":["Keep k6 (and its cloud SDK types) updated so decode targets match the deployed API schema.","Configure NO_PROXY for cloud hosts when a corporate proxy rewrites responses.","Log a body snippet on decode failure once — it immediately distinguishes HTML proxies from truncation."],"tags":["k6","cloud","http-client","json","response-decoding","schema-mismatch"],"backgroundTag":"invalid-json-response","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}