title: Using request_mock to dynamically set response based on request
body: <p>I am trying to mock a simple POST request that creates a resource from the request body, and returns the resource that was created. For simplicity, let's assume the created resource is exactly as passed in, but given an ID when created. Here is my code:</p>
  <pre><code>def test_create_resource(requests_mock):
      # Helper function to generate dynamic response
      def get_response(request, context):
          context.status_code = 201
          # I assumed this would contain the request body
          response = request.json()
          response['id'] = 100
          return response
  
      # Mock the response
      requests_mock.post('test-url/resource', json=get_response)
      resource = function_that_creates_resource()
      assert resource['id'] == 100
  </code></pre>
  <p>I end up with runtime error <code>JSONDecodeError('Expecting value: line 1 column 1 (char 0)')</code>. I assume this is because <code>request.json()</code> does not contain what I am looking for. How can I access the request body?</p>