Skip to content

Commit 4e7b687

Browse files
authored
Merge pull request RasaHQ#5322 from RasaHQ/fix-callback-resp
Fix callback resp
2 parents f87cc97 + 6e53fc6 commit 4e7b687

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

changelog/5230.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed issue where posting to certain callback channel URLs would return a 500 error on successful posts due to invalid response format

rasa/utils/endpoints.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33

44
import aiohttp
5+
from aiohttp.client_exceptions import ContentTypeError
56
from typing import Any, Optional, Text, Dict
67

78
from sanic.request import Request
@@ -119,10 +120,9 @@ async def request(
119120
method: Text = "post",
120121
subpath: Optional[Text] = None,
121122
content_type: Optional[Text] = "application/json",
122-
return_method: Text = "json",
123123
**kwargs: Any,
124-
):
125-
"""Send a HTTP request to the endpoint.
124+
) -> Optional[Any]:
125+
"""Send a HTTP request to the endpoint. Return json response, if available.
126126
127127
All additional arguments will get passed through
128128
to aiohttp's `session.request`."""
@@ -144,12 +144,15 @@ async def request(
144144
headers=headers,
145145
params=self.combine_parameters(kwargs),
146146
**kwargs,
147-
) as resp:
148-
if resp.status >= 400:
147+
) as response:
148+
if response.status >= 400:
149149
raise ClientResponseError(
150-
resp.status, resp.reason, await resp.content.read()
150+
response.status, response.reason, await response.content.read()
151151
)
152-
return await getattr(resp, return_method)()
152+
try:
153+
return await response.json()
154+
except ContentTypeError:
155+
return None
153156

154157
@classmethod
155158
def from_dict(cls, data) -> "EndpointConfig":

tests/utils/test_endpoints.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,19 @@ def test_endpoint_config_custom_token_name():
9494
actual = endpoint_utils.EndpointConfig.from_dict(test_data)
9595

9696
assert actual.token_name == "test_token"
97+
98+
99+
async def test_request_non_json_response():
100+
with aioresponses() as mocked:
101+
endpoint = endpoint_utils.EndpointConfig("https://example.com/")
102+
103+
mocked.post(
104+
"https://example.com/test",
105+
payload="ok",
106+
content_type="application/text",
107+
status=200,
108+
)
109+
110+
response = await endpoint.request("post", subpath="test",)
111+
112+
assert not response

0 commit comments

Comments
 (0)