Mitmproxy: TypeError: a bytes-like object is required, not 'str'

Created on 30 Oct 2017  路  4Comments  路  Source: mitmproxy/mitmproxy

Steps to reproduce the problem:

script.py:

def response(flow):
   if "customstring" in flow.response.content:
     >my code>

TypeError: a bytes-like object is required, not 'str'

I need to use b"customstring" instead "customstring". This script was working fine on mitmproxy 0.18 but fails on mitmproxy 2.0.0

Want to use the same script because is very long and was stable on older mitm version. Any way to use strings instead bytes?

Any other comments? What have you tried so far?
System information

Mitmproxy version: 3.0.0 (2.0.0dev0916-0x930d78b5)
Python version: 3.5.3
Platform: Linux-4.9.0-3-amd64-x86_64-with-debian-9.1
Linux distro: debian 9.1

Thanks!

Most helpful comment

I can't link to this from mobile, but take a look at get_text and get_content at http://docs.mitmproxy.org/en/stable/scripting/api.html#mitmproxy.http.HTTPResponse! .content is bytes, .text is the completely decoded version! Downside of using bytes is that malformed documents may raise an error unless you pass strict=False.

All 4 comments

I can't link to this from mobile, but take a look at get_text and get_content at http://docs.mitmproxy.org/en/stable/scripting/api.html#mitmproxy.http.HTTPResponse! .content is bytes, .text is the completely decoded version! Downside of using bytes is that malformed documents may raise an error unless you pass strict=False.

Thanks @mhils !

I'm trying with flow.response.text (instead .content) but no luck, :(

Done!

I was missing the following line:
from mitmproxy import http
and use
def response(flow: http.HTTPFlow) -> None:
instead
def response(flow)

Now .text is working! Thanks @mhils

For the record, the import for the type annotation shouldn't make a difference, but glad you made it work! 馃槂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adforcetestuser1 picture adforcetestuser1  路  5Comments

Dauth picture Dauth  路  3Comments

atx picture atx  路  4Comments

mhils picture mhils  路  4Comments

Ch1ngg picture Ch1ngg  路  3Comments