diff --git a/changelog/912.improvement.md b/changelog/912.improvement.md new file mode 100644 index 000000000..962676632 --- /dev/null +++ b/changelog/912.improvement.md @@ -0,0 +1,3 @@ +Added the ability to decompress deflate encoded +json payloads for the webhook endpoint of the +action server. \ No newline at end of file diff --git a/rasa_sdk/endpoint.py b/rasa_sdk/endpoint.py index 9b1aaccf0..9a846bd39 100644 --- a/rasa_sdk/endpoint.py +++ b/rasa_sdk/endpoint.py @@ -2,6 +2,8 @@ import logging import os import types +import zlib +import json from typing import List, Text, Union, Optional, Any from ssl import SSLContext @@ -90,7 +92,13 @@ async def health(_) -> HTTPResponse: @app.post("/webhook") async def webhook(request: Request) -> HTTPResponse: """Webhook to retrieve action calls.""" - action_call = request.json + if request.headers.get("Content-Encoding") == "deflate": + # Decompress the request data using zlib + decompressed_data = zlib.decompress(request.body) + # Load the JSON data from the decompressed request data + action_call = json.loads(decompressed_data) + else: + action_call = request.json if action_call is None: body = {"error": "Invalid body request"} return response.json(body, status=400) diff --git a/tests/test_endpoint.py b/tests/test_endpoint.py index 7ce1d48c5..9cc082707 100644 --- a/tests/test_endpoint.py +++ b/tests/test_endpoint.py @@ -1,5 +1,6 @@ import pytest import json +import zlib import rasa_sdk.endpoint as ep from rasa_sdk.events import SlotSet @@ -90,3 +91,21 @@ def test_arg_parser_actions_params_module_style(): args = ["--actions", "actions.act"] cmdline_args = parser.parse_args(args) assert cmdline_args.actions == "actions.act" + + +def test_server_webhook_custom_action_encoded_data_returns_200(): + data = { + "next_action": "custom_action", + "tracker": {"sender_id": "1", "conversation_id": "default"}, + "domain": {"intents": ["greet", "goodbye"]}, + } + + request, response = app.test_client.post( + "/webhook", + data=zlib.compress(json.dumps(data).encode()), + headers={"Content-encoding": "deflate"}, + ) + events = response.json.get("events") + + assert events == [SlotSet("test", "bar")] + assert response.status == 200