{"record":{"id":"152423ce2860dd9d","repo":"HKUDS/Vibe-Trading","slug":"trading-212-api-returned-invalid-json","errorCode":null,"errorMessage":"Trading 212 API returned invalid JSON.","messagePattern":"Trading 212 API returned invalid JSON\\.","errorType":"http","errorClass":"Trading212APIError","httpStatus":null,"severity":"error","filePath":"agent/src/trading/connectors/trading212/sdk.py","lineNumber":438,"sourceCode":"            url,\n            headers=headers,\n            auth=auth,\n            params=dict(params or {}),\n            timeout=config.timeout,\n        )\n    except requests.RequestException as exc:\n        raise Trading212APIError(f\"Trading 212 request failed: {exc}\") from exc\n\n    if response.status_code in (401, 403):\n        raise Trading212APIError(\"Trading 212 API authentication failed: check api_key/api_secret.\")\n    if response.status_code >= 400:\n        raise Trading212APIError(f\"Trading 212 API returned HTTP {response.status_code}: {_error_message(response)}\")\n    if not response.content:\n        return None\n    try:\n        return response.json()\n    except ValueError as exc:\n        raise Trading212APIError(\"Trading 212 API returned invalid JSON.\") from exc\n\n\ndef _error_message(response: requests.Response) -> str:\n    try:\n        payload = response.json()\n    except ValueError:\n        return response.text.strip() or response.reason or \"request failed\"\n    if isinstance(payload, Mapping):\n        for key in (\"message\", \"error\", \"detail\", \"title\"):\n            value = payload.get(key)\n            if value:\n                return str(value)\n    return str(payload)\n\n\ndef _missing_fields(config: Trading212Config) -> list[str]:\n    missing = []\n    if not config.api_key:","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/trading/connectors/trading212/sdk.py#L420-L456","documentation":"Raised by _request when the HTTP response has content but response.json() raises ValueError, i.e. the server returned a 2xx body that is not valid JSON. The chained exception holds the parser detail.","triggerScenarios":"Any _get call where the API (or an intermediary proxy) returns 200 with an HTML error page, plain-text body, truncated JSON, or a gateway interstitial instead of JSON.","commonSituations":"Captive portal / proxy injecting an HTML page; CDN or WAF challenge page; truncated response on flaky connections; API incident returning non-JSON payloads.","solutions":["Retry the request — truncated/garbled responses are often transient","Log response headers and a body snippet to identify what non-JSON payload came back (proxy page vs API body)","If a proxy/WAF is intercepting, bypass or configure it for the Trading 212 host","Report/check the Trading 212 status page if it persists account-wide"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    data = get_account_metadata(config)\nexcept Trading212APIError as exc:\n    if \"invalid JSON\" in str(exc):\n        data = get_account_metadata(config)  # one retry; often transient truncation\n    else:\n        raise","preventionTips":["Retry once on invalid-JSON responses before escalating","Log response Content-Type/headers when debugging proxy interference","Avoid environments with SSL-inspecting proxies for API traffic"],"tags":["trading212","json","response-parsing","http"],"backgroundTag":"invalid-json-response","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}