Fix crashing when editing form with random data, fix #2794
diff --git a/mitmproxy/net/http/request.py b/mitmproxy/net/http/request.py
index 6b4041f6f2..959fdd3399 100644
--- a/mitmproxy/net/http/request.py
+++ b/mitmproxy/net/http/request.py
@@ -429,10 +429,7 @@ def constrain_encoding(self):
     def _get_urlencoded_form(self):
         is_valid_content_type = "application/x-www-form-urlencoded" in self.headers.get("content-type", "").lower()
         if is_valid_content_type:
-            try:
-                return tuple(mitmproxy.net.http.url.decode(self.content.decode()))
-            except ValueError:
-                pass
+            return tuple(mitmproxy.net.http.url.decode(self.get_text(strict=False)))
         return ()
 
     def _set_urlencoded_form(self, form_data):
@@ -441,7 +438,7 @@ def _set_urlencoded_form(self, form_data):
         This will overwrite the existing content if there is one.
         """
         self.headers["content-type"] = "application/x-www-form-urlencoded"
-        self.content = mitmproxy.net.http.url.encode(form_data, self.content.decode()).encode()
+        self.content = mitmproxy.net.http.url.encode(form_data, self.get_text(strict=False)).encode()
 
     @property
     def urlencoded_form(self):
diff --git a/test/mitmproxy/net/http/test_request.py b/test/mitmproxy/net/http/test_request.py
index ce49002c06..ef581a9147 100644
--- a/test/mitmproxy/net/http/test_request.py
+++ b/test/mitmproxy/net/http/test_request.py
@@ -351,10 +351,10 @@ def test_get_urlencoded_form(self):
         request.headers["Content-Type"] = "application/x-www-form-urlencoded"
         assert list(request.urlencoded_form.items()) == [("foobar", "baz")]
         request.raw_content = b"\xFF"
-        assert len(request.urlencoded_form) == 0
+        assert len(request.urlencoded_form) == 1
 
     def test_set_urlencoded_form(self):
-        request = treq()
+        request = treq(content=b"\xec\xed")
         request.urlencoded_form = [('foo', 'bar'), ('rab', 'oof')]
         assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"
         assert request.content

Fixes #2794 