# Issue with create\_request and JSON

**URL:** <https://discourse.mitmproxy.org/t/issue-with-create-request-and-json/281>\
**Category:** Uncategorized\
**Created:** [December 31, 2016, 3:45pm UTC](https://discourse.mitmproxy.org/t/issue-with-create-request-and-json/281 "2016-12-31T15:45:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mk1Escort](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Mk1Escort](https://discourse.mitmproxy.org/u/Mk1Escort)\
**Post date:** [December 31, 2016, 3:45pm UTC](https://discourse.mitmproxy.org/t/issue-with-create-request-and-json/281/1 "2016-12-31T15:45:22Z")

</div>

As the title says, I’m having issues with create\_request and JSON using mitmproxy 1.0.1

My code so far:

```
from mitmproxy import ctx
import json
def request(flow):
    if flow.request.host.startswith("flex-capacity"):
        data = json.loads(flow.response.text)
        f = ctx.master.create_request(method='GET', scheme='http', host='www.example.com', path='/', port=80)
        flow.request.headers["Content-Type"] = "application/json"
        flow.request.content = json.dumps(['foo', {'bar': ('baz')}])
        ctx.master.view.add(f)
        ctx.master.replay_request(f, block=False)

```

I’m monitoring content sent via an Android app. I want to match a host from a request sent by the app and then send a created request with a bit of JSON. If the script is ran with `flow.response.content`, it errors and says JSON expects a string, not bytes. If I run the script on flow sent by the app, an empty request is created–the header and body are **not** changed. If I re-run the script on the created request, it fills out the header and body as expected.

Running the script at startup with the -s switch results in unique behavior. If the JSON references are commented out, the new request is created and filled out as expected. Leave the JSON references in, and it gives an error that NoneType object has no attribute ‘text’. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [December 31, 2016, 4:00pm UTC](https://discourse.mitmproxy.org/t/issue-with-create-request-and-json/281/2 "2016-12-31T16:00:13Z")

</div>

You are trying to access to response in the `request()` hook - that likely won’t work. 🙂

---

<div class="post-metadata">

**Author:** ![Mk1Escort](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Mk1Escort](https://discourse.mitmproxy.org/u/Mk1Escort)\
**Post date:** [December 31, 2016, 4:18pm UTC](https://discourse.mitmproxy.org/t/issue-with-create-request-and-json/281/3 "2016-12-31T16:18:30Z")

</div>

That did it. This may not mean much to you, but right now you’re my favorite person.
