From abaeb7bfc42d725fd10119d1ee8584c0cf88c376 Mon Sep 17 00:00:00 2001 From: Evan Tahler Date: Thu, 14 Nov 2024 19:18:08 +0000 Subject: [PATCH 1/2] Handle HTTPX UTF-8 decoding errors --- vcr/stubs/httpx_stubs.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vcr/stubs/httpx_stubs.py b/vcr/stubs/httpx_stubs.py index 759cb727..8a8427cb 100644 --- a/vcr/stubs/httpx_stubs.py +++ b/vcr/stubs/httpx_stubs.py @@ -2,6 +2,7 @@ import functools import inspect import logging +import warnings from unittest.mock import MagicMock, patch import httpx @@ -105,7 +106,12 @@ def _from_serialized_response(request, serialized_response, history=None): def _make_vcr_request(httpx_request, **kwargs): - body = httpx_request.read().decode("utf-8") + try: + body = httpx_request.read().decode("utf-8") + except UnicodeDecodeError e: + body = httpx_request.read().decode("utf-8", errors="ignore") + warnings.warn(f"Could not decode full request payload as UTF8, recording may have lost bytes. {e}") + uri = str(httpx_request.url) headers = dict(httpx_request.headers) return VcrRequest(httpx_request.method, uri, body, headers) From b03180f8bb2eacfacb47465675dfa16a941bc854 Mon Sep 17 00:00:00 2001 From: Evan Tahler Date: Thu, 14 Nov 2024 11:27:34 -0800 Subject: [PATCH 2/2] Update httpx_stubs.py --- vcr/stubs/httpx_stubs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcr/stubs/httpx_stubs.py b/vcr/stubs/httpx_stubs.py index 8a8427cb..df729d6d 100644 --- a/vcr/stubs/httpx_stubs.py +++ b/vcr/stubs/httpx_stubs.py @@ -108,7 +108,7 @@ def _from_serialized_response(request, serialized_response, history=None): def _make_vcr_request(httpx_request, **kwargs): try: body = httpx_request.read().decode("utf-8") - except UnicodeDecodeError e: + except UnicodeDecodeError as e: body = httpx_request.read().decode("utf-8", errors="ignore") warnings.warn(f"Could not decode full request payload as UTF8, recording may have lost bytes. {e}")