title: Mitmproxy tampering with GET and POST request/ response in one script
body: <p>A POST request sent to a certain URL (<code>http://test.com</code>) looks like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;messageType&quot;: &quot;OK&quot;,
      &quot;city&quot;: {
          &quot;Name&quot;: &quot;Paris&quot;,
          &quot;Views&quot;: {
              &quot;1231&quot;: {
                  &quot;id&quot;: 4234,
                   &quot;enableView&quot;: false
              },
          },
          &quot;Views&quot;: [5447, 8457],
          &quot;messages&quot;: [{
              &quot;id&quot;: &quot;message_6443&quot;,
              &quot;eTag&quot;: 756754338
          }]
      },
      &quot;client&quot;: {
          &quot;Id&quot;: 53,
          &quot;email&quot;: &quot;test@test.us&quot;,
          &quot;firstName&quot;: &quot;test&quot;,
          &quot;lastName&quot;: &quot;test&quot;,
          &quot;id&quot;: 52352352,
          &quot;uuid&quot;: &quot;5631f-grdeh4&quot;,
          &quot;isAdmin&quot;: false
      }
  }
  </code></pre>
  <p>I need to intercept the request and change <code>isAdmin</code> to true.</p>
  <p>And a GET request to a certain URL (<code>https://test.com/profiles/{Random_Numbers}/{id}</code>) has a (encoded) response like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;id&quot;: 0, 
      &quot;Code&quot;: &quot;Admin&quot;, 
      &quot;display&quot;: &quot;RRRR&quot;
  }
  </code></pre>
  <p>I need to change <code>id</code> to 5.</p>
  <p>So basically I need to write one script that will do both of these actions.</p>
  <p>So far I have tried to take advantage of some examples on GitHub, but I haven't gotten it so far:</p>
  <pre class="lang-python prettyprint-override"><code>from libmproxy.protocol.http import decoded
  
  def start(context, argv):
    if len(argv) != 3:
     raise ValueError('Usage: -s &quot;modify-response-body.py old new&quot;')
      context.old, context.new = argv[1], argv[2]
  
  
  def response(context, flow):
      with decoded(flow.response):  # automatically decode gzipped responses.
        flow.response.content = flow.response.content.replace(context.old, context.new)`
  </code></pre>
  <p>How do I implement this for my scenario?</p>
  <p>Probably using the libmproxy to get http-request and response would be a better idea, maybe.</p>