I have tried everything and it doesn’t work. I tried to write the json response into a file but file is always empty. please help!
I resume this thread.
My purpose is the same as alfonsorod2003 but in my script I get error when I try to access the text property of a response. The error is
error when decoding b’\xac\x18 with ‘deflate’: error(‘Error -5 while decompressing data: incomplete or truncated stream’,)
I can normally get raw_content.
If I start mitmproxy with -z option my script works without error and the client receives modified response.
-
.raw_content
: The HTTP message body exactly as we receive it on the wire. -
.content
: The HTTP message body decoded with the content-encoding header (e.g. gzip). This is still a Python bytes object. -
.text
: The HTTP message body decoded with both content-encoding header (e.g. gzip) and content-type header charset. This is a Python str.
What gives?
- Accessing
.raw_content
never fails. - Accessing
.content
may fail in the unlikely event that the server messes up the Content-Encoding. - Accessing
.text
may regularly fail because many websites don’t specify proper content types.
For 2 and 3, there are non-strict variants available as get_content(strict=False)
and get_text(strict=False)
.