kevin1024 / vcrpy

Automatically mock your HTTP interactions to simplify and speed up testing
MIT License
2.68k stars 386 forks source link

String decoding fails for POST requests with byte payload #844

Open thomas-maschler opened 3 months ago

thomas-maschler commented 3 months ago

One of my requests I am trying to record uploads an image to cloud storage.

The POST requests' payload is a byte-string (not a BytesIO object).

The body contains some non-utf-8 characters and when trying to decode them, function replace_post_data_parameters breaks

  File "/.../site-packages/vcr/filters.py", line 116, in replace_post_data_parameters
    rk = k.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 0: invalid start byte
JonLev commented 2 months ago

On the same idea, I have the issue of using Whisper model from OpenAI, using their official librairy, the client is HTTPx, and it send a post with bytes data. I have the same failure at a different place: https://github.com/kevin1024/vcrpy/blob/master/vcr/stubs/httpx_stubs.py#L108

body = httpx_request.read().decode("utf-8")

So the questions are: Is this decode necessary at this moment ? How can we use VCRPY in these cases ?

thomas-maschler commented 2 months ago

I got it to work with this patch https://github.com/kevin1024/vcrpy/compare/master...thomas-maschler:vcrpy:decode

fiendish commented 2 months ago

This looks like a duplicate of https://github.com/kevin1024/vcrpy/issues/660

ddorian commented 2 months ago

Probably needs this too https://github.com/kevin1024/vcrpy/commit/1ef75a1e3f7a34b500cdecf4f06e0fced38457eb