{"record":{"id":"1aed512945ac6f56","repo":"FujiwaraChoki/MoneyPrinterV2","slug":"post-bridge-returned-a-non-json-response","errorCode":null,"errorMessage":"Post Bridge returned a non-JSON response.","messagePattern":"Post Bridge returned a non-JSON response\\.","errorType":"http","errorClass":"PostBridgeClientError","httpStatus":null,"severity":"error","filePath":"src/classes/PostBridge.py","lineNumber":190,"sourceCode":"        return self._request_json(\n            \"POST\",\n            f\"{self.API_BASE}/posts\",\n            json=payload,\n        )\n\n    def _guess_mime_type(self, file_path: str) -> str:\n        guessed_type = mimetypes.guess_type(file_path)[0]\n        if guessed_type in {\"image/png\", \"image/jpeg\", \"video/mp4\", \"video/quicktime\"}:\n            return guessed_type\n        return \"video/mp4\"\n\n    def _request_json(self, method: str, url: str, **kwargs) -> dict:\n        response = self._request(method, url, **kwargs)\n\n        try:\n            response_json = response.json()\n        except ValueError as exc:\n            raise PostBridgeClientError(\n                \"Post Bridge returned a non-JSON response.\",\n                status_code=response.status_code,\n            ) from exc\n\n        if not isinstance(response_json, dict):\n            return {\"data\": response_json}\n\n        return response_json\n\n    def _request(\n        self,\n        method: str,\n        url: str,\n        *,\n        headers: Optional[dict] = None,\n        timeout: int = 60,\n        expected_statuses: Optional[set[int]] = None,\n        use_default_headers: bool = True,","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/FujiwaraChoki/MoneyPrinterV2/blob/5192af8eca97759f941834b947e3ceb209da0649/src/classes/PostBridge.py#L172-L208","documentation":"Raised by PostBridgeClient._request_json when the HTTP response body cannot be parsed as JSON (requests' response.json() raises ValueError). This usually means the server or an intermediary returned HTML/plain text (error page, CDN block, auth redirect) instead of the expected JSON.","triggerScenarios":"A 200/4xx/5xx response with an HTML error page from Cloudflare or a gateway, an XML/SOAP error from a misrouted endpoint, an empty body, or hitting the wrong host (e.g. typo in base URL serving a static site).","commonSituations":"Rate limiting or WAF challenges returning HTML with status 200, misconfigured base_url in config, API maintenance pages, or transparent proxies altering responses.","solutions":["Log response.status_code and response.text[:200] to see what actually came back","Check whether the base URL and API token are correct (auth redirects often return HTML)","Retry with backoff — transient gateway/WAF pages often clear up","If a WAF/Cloudflare challenge persists, add appropriate headers or a User-Agent, or contact the API provider"],"exampleFix":"// before\ntry:\n    response_json = response.json()\nexcept ValueError as exc:\n    raise PostBridgeClientError(\"Post Bridge returned a non-JSON response.\") from exc\n\n// after\ntry:\n    response_json = response.json()\nexcept ValueError as exc:\n    raise PostBridgeClientError(\n        f\"Non-JSON response (status={response.status_code}): {response.text[:200]!r}\",\n        status_code=response.status_code,\n    ) from exc","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"def looks_like_json_response(response) -> bool:\n    content_type = response.headers.get(\"Content-Type\", \"\")\n    return \"application/json\" in content_type or response.text.lstrip().startswith((\"{\", \"[\"))","tryCatchPattern":"try:\n    result = client.list_social_accounts()\nexcept PostBridgeClientError as exc:\n    if \"non-JSON response\" in str(exc):\n        logging.warning(\"Transient gateway/WAF page; backing off and retrying\")\n        time.sleep(5)\n        result = client.list_social_accounts()\n    else:\n        raise","preventionTips":["Check Content-Type headers before parsing when the API supports it","Include retry-with-backoff for HTML error pages from CDNs/WAFs","Validate base_url and token configuration so auth-redirect HTML is never fetched"],"tags":["python","api-client","json","http"],"backgroundTag":"non-json-api-response","analyzedSha":"5192af8eca97759f941834b947e3ceb209da0649","analyzedAt":"2026-08-28T10:31:46.474Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}