# Buggy code
async def request(
    self,
    method: Text = "post",
    subpath: Optional[Text] = None,
    content_type: Optional[Text] = "application/json",
    return_method: Text = "json",
    **kwargs: Any,
):
    """Send a HTTP request to the endpoint.

    All additional arguments will get passed through
    to aiohttp's `session.request`."""

    # create the appropriate headers
    headers = {}
    if content_type:
        headers["Content-Type"] = content_type

    if "headers" in kwargs:
        headers.update(kwargs["headers"])
        del kwargs["headers"]

    url = concat_url(self.url, subpath)
    async with self.session() as session:
        async with session.request(
            method,
            url,
            headers=headers,
            params=self.combine_parameters(kwargs),
            **kwargs,
        ) as resp:
            if resp.status >= 400:
                raise ClientResponseError(
                    resp.status, resp.reason, await resp.content.read()
                )
            return await getattr(resp, return_method)()


# Traceback (aiohttp.client_exceptions.ContentTypeError)
2020-02-07 10:12:19 DEBUG    rasa.server  - Traceback (most recent call last):
File "/build/lib/python3.6/site-packages/rasa/server.py", line 663, in trigger_intent
tracker=tracker,
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/agent.py", line 535, in trigger_intent
intent_name, entities, tracker, output_channel,
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/processor.py", line 378, in trigger_external_user_uttered
await self._predict_and_execute_next_action(output_channel, tracker)
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/processor.py", line 509, in _predict_and_execute_next_action
action, tracker, output_channel, self.nlg, policy, confidence
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/processor.py", line 620, in _run_action
await self._send_bot_messages(events, tracker, output_channel)
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/processor.py", line 549, in _send_bot_messages
await output_channel.send_response(tracker.sender_id, e.message())
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/channels/channel.py", line 186, in send_response
await self.send_text_message(recipient_id, message.pop("text"), **message)
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/channels/channel.py", line 328, in send_text_message
await self._persist_message(self._message(recipient_id, text=message_part))
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/channels/callback.py", line 34, in _persist_message
"post", content_type="application/json", json=message
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/utils/endpoints.py", line 152, in request
return await getattr(resp, return_method)()
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 1027, in json
headers=self.headers)
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html'

# Fixed code
async def request(
    self,
    method: Text = "post",
    subpath: Optional[Text] = None,
    content_type: Optional[Text] = "application/json",
    **kwargs: Any,
) -> Optional[Any]:
    """Send a HTTP request to the endpoint. Return json response, if available.

    All additional arguments will get passed through
    to aiohttp's `session.request`."""

    # create the appropriate headers
    headers = {}
    if content_type:
        headers["Content-Type"] = content_type

    if "headers" in kwargs:
        headers.update(kwargs["headers"])
        del kwargs["headers"]

    url = concat_url(self.url, subpath)
    async with self.session() as session:
        async with session.request(
            method,
            url,
            headers=headers,
            params=self.combine_parameters(kwargs),
            **kwargs,
        ) as response:
            if response.status >= 400:
                raise ClientResponseError(
                    response.status, response.reason, await response.content.read()
                )
            try:
                return await response.json()
            except ContentTypeError:
                return None