title: Unit testing Python Azure Function: How do I construct a mock test request message with a JSON payload?
body: <p>I want to unit test my Python Azure function. I'm following the <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python#unit-testing" rel="nofollow noreferrer">Microsoft documentation</a>.</p>
  <p>The documentation mocks the call to the function as follows</p>
  <pre class="lang-py prettyprint-override"><code>req = func.HttpRequest(
              method='GET',
              body=None,
              url='/api/HttpTrigger',
              params={'name': 'Test'})
  </code></pre>
  <p>I would like to do this but with the parameters passed as a JSON object so that I can follow the <code>req_body = req.get_json()</code> branch of the function code. I guessed I would be able to do this with a function call like</p>
  <pre class="lang-py prettyprint-override"><code>req = func.HttpRequest(
              method='GET',
              body=json.dumps({'name': 'Test'}),
              url='/api/HttpTrigger',
              params=None)
  </code></pre>
  <p>If I construct the call like this, <code>req.get_json()</code> fails with the error message <code>AttributeError: 'str' object has no attribute 'decode'</code>.</p>
  <p>How do I construct the request with JSON input parameters? It should be trivial but I'm clearly missing something obvious.</p>