How do i edit a decoded deflated Json response using python script?


#1

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! :frowning:


#2

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.


#3
  • .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?

  1. Accessing .raw_content never fails.
  2. Accessing .content may fail in the unlikely event that the server messes up the Content-Encoding.
  3. 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).