FujiwaraChoki/MoneyPrinterV2 · error · PostBridgeClientError

Post Bridge returned a non-JSON response.

Error message

Post Bridge returned a non-JSON response.

What it means

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.

Source

Thrown at src/classes/PostBridge.py:190

        return self._request_json(
            "POST",
            f"{self.API_BASE}/posts",
            json=payload,
        )

    def _guess_mime_type(self, file_path: str) -> str:
        guessed_type = mimetypes.guess_type(file_path)[0]
        if guessed_type in {"image/png", "image/jpeg", "video/mp4", "video/quicktime"}:
            return guessed_type
        return "video/mp4"

    def _request_json(self, method: str, url: str, **kwargs) -> dict:
        response = self._request(method, url, **kwargs)

        try:
            response_json = response.json()
        except ValueError as exc:
            raise PostBridgeClientError(
                "Post Bridge returned a non-JSON response.",
                status_code=response.status_code,
            ) from exc

        if not isinstance(response_json, dict):
            return {"data": response_json}

        return response_json

    def _request(
        self,
        method: str,
        url: str,
        *,
        headers: Optional[dict] = None,
        timeout: int = 60,
        expected_statuses: Optional[set[int]] = None,
        use_default_headers: bool = True,

View on GitHub (pinned to 5192af8eca)

Solutions

  1. Log response.status_code and response.text[:200] to see what actually came back
  2. Check whether the base URL and API token are correct (auth redirects often return HTML)
  3. Retry with backoff — transient gateway/WAF pages often clear up
  4. If a WAF/Cloudflare challenge persists, add appropriate headers or a User-Agent, or contact the API provider

Example fix

// before
try:
    response_json = response.json()
except ValueError as exc:
    raise PostBridgeClientError("Post Bridge returned a non-JSON response.") from exc

// after
try:
    response_json = response.json()
except ValueError as exc:
    raise PostBridgeClientError(
        f"Non-JSON response (status={response.status_code}): {response.text[:200]!r}",
        status_code=response.status_code,
    ) from exc
Defensive patterns

Strategy: try-catch

Type guard

def looks_like_json_response(response) -> bool:
    content_type = response.headers.get("Content-Type", "")
    return "application/json" in content_type or response.text.lstrip().startswith(("{", "["))

Try / catch

try:
    result = client.list_social_accounts()
except PostBridgeClientError as exc:
    if "non-JSON response" in str(exc):
        logging.warning("Transient gateway/WAF page; backing off and retrying")
        time.sleep(5)
        result = client.list_social_accounts()
    else:
        raise

Prevention

When it happens

Trigger: 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).

Common situations: Rate limiting or WAF challenges returning HTML with status 200, misconfigured base_url in config, API maintenance pages, or transparent proxies altering responses.

Related errors


AI-assisted analysis of FujiwaraChoki/MoneyPrinterV2@5192af8eca (2026-08-28). Data as JSON: /api/errors/1aed512945ac6f56. Report an issue: GitHub.