{"record":{"id":"4043cd4c26117594","repo":"locustio/locust","slug":"if-you-want-to-change-the-state-of-the-request-usi","errorCode":null,"errorMessage":"If you want to change the state of the request using .success() or .failure(), you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses","messagePattern":"If you want to change the state of the request using \\.success\\(\\) or \\.failure\\(\\), you must pass catch_response=True\\. See http://docs\\.locust\\.io/en/stable/writing-a-locustfile\\.html#validating-responses","errorType":"exception","errorClass":"LocustError","httpStatus":null,"severity":"error","filePath":"locust/clients.py","lineNumber":539,"sourceCode":"                conn.cert_file = cert\n                conn.key_file = None\n            if conn.cert_file and not os.path.exists(conn.cert_file):\n                raise OSError(f\"Could not find the TLS certificate file, invalid path: {conn.cert_file}\")\n            if conn.key_file and not os.path.exists(conn.key_file):\n                raise OSError(f\"Could not find the TLS key file, invalid path: {conn.key_file}\")\n\n    def build_connection_pool_key_attributes(self, request, verify, cert=None):\n        host_params, pool_kwargs = super().build_connection_pool_key_attributes(request, verify, cert)\n\n        if requests_version >= (2, 32, 5) and verify is True:\n            pool_kwargs[\"ssl_context\"] = _preloaded_ssl_context\n\n        return host_params, pool_kwargs\n\n\n# Monkey patch Response class to give some guidance\ndef _missing_catch_response_True(self, *_args, **_kwargs):\n    raise LocustError(\n        \"If you want to change the state of the request using .success() or .failure(), you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses\"\n    )\n\n\nResponse.success = _missing_catch_response_True  # type: ignore[attr-defined]\nResponse.failure = _missing_catch_response_True  # type: ignore[attr-defined]\n","sourceCodeStart":521,"sourceCodeEnd":546,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/clients.py#L521-L546","documentation":"Locust monkey-patches requests.Response.success/failure to raise LocustError with guidance, so calls on a plain (non-catch_response) requests Response fail fast. This catches the common mistake of calling success()/failure() on a Response returned without catch_response=True, where manual marking is unsupported.","triggerScenarios":"Calling `.success()` or `.failure()` on the Response from `self.client.get(...)` (HttpSession) when `catch_response=True` was not passed.","commonSituations":"Forgetting catch_response=True when adding validation; IDE autocomplete suggesting success()/failure() on requests.Response; sharing validation code between catch and non-catch calls.","solutions":["Add `catch_response=True` to the request call","Keep validation logic only on requests created with catch_response=True","Use `response.raise_for_status()` or status_code checks without manual marking when catch_response is not needed"],"exampleFix":"// before\nresponse = self.client.get(\"/api\")\nif response.status_code == 200:\n    response.success()  # raises\n// after\nwith self.client.get(\"/api\", catch_response=True) as response:\n    if response.status_code == 200:\n        response.success()","handlingStrategy":"validation","validationCode":"def mark_success(client, url, **kw):\n    kw.setdefault(\"catch_response\", True)\n    with client.get(url, **kw) as resp:\n        resp.success()","typeGuard":"null","tryCatchPattern":"try:\n    with client.get(url, catch_response=True) as resp:\n        resp.success()\nexcept LocustError as e:\n    logger.warning(\"catch_response missing: %s\", e)","preventionTips":["Adopt a helper that always sets catch_response=True for validated requests","Never call success()/failure() on plain requests.Response","Document the flag in team locustfile templates"],"tags":["locust","http","catch-response","monkey-patch"],"backgroundTag":"catch-response-required","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}