{"record":{"id":"855f91f73d95c16e","repo":"locustio/locust","slug":"in-order-to-use-a-with-block-for-requests-you-mus","errorCode":null,"errorMessage":"In order to use a with-block for requests, you must also pass catch_response=True","messagePattern":"In order to use a with-block for requests, you must also pass catch_response=True","errorType":"exception","errorClass":"LocustError","httpStatus":null,"severity":"error","filePath":"locust/clients.py","lineNumber":391,"sourceCode":"\n    @classmethod\n    def wrap_response(\n        cls, response: Response, request_event: EventHook, request_meta: Mapping[str, Any], catch_response: bool\n    ) -> ResponseContextManager:\n        \"\"\"Modify a Response object in-place, for efficiency reasons\"\"\"\n        response.__class__ = ResponseContextManager\n        response = cast(ResponseContextManager, response)\n        response._entered = False\n        response._manual_result = None\n        response._request_event = request_event\n        response._catch_response = catch_response\n        response.request_meta = request_meta\n        return response\n\n    def __enter__(self):\n        self._entered = True\n        if not self._catch_response:\n            raise LocustError(\"In order to use a with-block for requests, you must also pass catch_response=True\")\n        return self\n\n    def __exit__(self, exc, value, traceback):  # type: ignore[override]\n        # if the user has already manually marked this response as failure or success\n        # we can ignore the default behaviour of letting the response code determine the outcome\n        if self._manual_result is not None:\n            if self._manual_result is True:\n                self.request_meta[\"exception\"] = None\n            elif isinstance(self._manual_result, Exception):\n                self.request_meta[\"exception\"] = self._manual_result\n            self._report_request()\n            return exc is None\n\n        if exc:\n            if isinstance(value, ResponseError):\n                self.request_meta[\"exception\"] = value\n                self._report_request()\n            else:","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/clients.py#L373-L409","documentation":"Locust's HttpSession request methods return a context-manager wrapper only when catch_response=True is passed. Entering a with-block without that flag means the request would not support manual success/failure marking, so __enter__ raises LocustError immediately. The library throws this to enforce that response validation is explicitly opted into.","triggerScenarios":"Calling `with self.client.get(...)` / `.post(...)` etc. (any with-block on an HttpSession request) while omitting `catch_response=True` in the request kwargs.","commonSituations":"Developers copy the with-block syntax from docs/examples but forget the flag; refactoring a plain request into a with-block to add validation; typos like `catch_response=false` or passing it to the wrong call.","solutions":["Add `catch_response=True` to the request call inside the with-block","If no validation is needed, drop the with-block and call the request directly, e.g. `self.client.get(url)`","Check spelling/casing of the `catch_response` kwarg"],"exampleFix":"// before\nwith self.client.get(\"/api\") as response:\n    if response.status_code == 200:\n        response.success()\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 validate_with_block_kwargs(kwargs):\n    if not kwargs.get(\"catch_response\"):\n        raise ValueError(\"with-block requires catch_response=True\")","typeGuard":"null","tryCatchPattern":"try:\n    with self.client.get(url, catch_response=True) as resp:\n        ...\nexcept LocustError as e:\n    logger.error(\" misuse of with-block: %s\", e)","preventionTips":["Always pair with-blocks with catch_response=True","Enable lint/review check for client.get inside `with` without kwargs","Prefer plain request calls when no validation is needed"],"tags":["locust","http","catch-response","context-manager"],"backgroundTag":"catch-response-required","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}