Skip to content

Handle HTTPX UTF-8 decoding errors #882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion vcr/stubs/httpx_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import functools
import inspect
import logging
import warnings
from unittest.mock import MagicMock, patch

import httpx
Expand Down Expand Up @@ -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 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}")

uri = str(httpx_request.url)
headers = dict(httpx_request.headers)
return VcrRequest(httpx_request.method, uri, body, headers)
Expand Down