Просмотр исходного кода

feat: add RequestBodyError for invalid request body handling (#11994)

Signed-off-by: -LAN- <laipz8200@outlook.com>
-LAN- 4 месяцев назад
Родитель
Сommit
e068bbec73

+ 4 - 0
api/core/workflow/nodes/http_request/exc.py

@@ -16,3 +16,7 @@ class InvalidHttpMethodError(HttpRequestNodeError):
 
 class ResponseSizeError(HttpRequestNodeError):
     """Raised when the response size exceeds the allowed threshold."""
+
+
+class RequestBodyError(HttpRequestNodeError):
+    """Raised when the request body is invalid."""

+ 9 - 0
api/core/workflow/nodes/http_request/executor.py

@@ -23,6 +23,7 @@ from .exc import (
     FileFetchError,
     HttpRequestNodeError,
     InvalidHttpMethodError,
+    RequestBodyError,
     ResponseSizeError,
 )
 
@@ -143,13 +144,19 @@ class Executor:
                 case "none":
                     self.content = ""
                 case "raw-text":
+                    if len(data) != 1:
+                        raise RequestBodyError("raw-text body type should have exactly one item")
                     self.content = self.variable_pool.convert_template(data[0].value).text
                 case "json":
+                    if len(data) != 1:
+                        raise RequestBodyError("json body type should have exactly one item")
                     json_string = self.variable_pool.convert_template(data[0].value).text
                     json_object = json.loads(json_string, strict=False)
                     self.json = json_object
                     # self.json = self._parse_object_contains_variables(json_object)
                 case "binary":
+                    if len(data) != 1:
+                        raise RequestBodyError("binary body type should have exactly one item")
                     file_selector = data[0].file
                     file_variable = self.variable_pool.get_file(file_selector)
                     if file_variable is None:
@@ -317,6 +324,8 @@ class Executor:
             elif self.json:
                 body = json.dumps(self.json)
             elif self.node_data.body.type == "raw-text":
+                if len(self.node_data.body.data) != 1:
+                    raise RequestBodyError("raw-text body type should have exactly one item")
                 body = self.node_data.body.data[0].value
         if body:
             raw += f"Content-Length: {len(body)}\r\n"

+ 5 - 1
api/core/workflow/nodes/http_request/node.py

@@ -20,7 +20,7 @@ from .entities import (
     HttpRequestNodeTimeout,
     Response,
 )
-from .exc import HttpRequestNodeError
+from .exc import HttpRequestNodeError, RequestBodyError
 
 HTTP_REQUEST_DEFAULT_TIMEOUT = HttpRequestNodeTimeout(
     connect=dify_config.HTTP_REQUEST_MAX_CONNECT_TIMEOUT,
@@ -136,9 +136,13 @@ class HttpRequestNode(BaseNode[HttpRequestNodeData]):
             data = node_data.body.data
             match body_type:
                 case "binary":
+                    if len(data) != 1:
+                        raise RequestBodyError("invalid body data, should have only one item")
                     selector = data[0].file
                     selectors.append(VariableSelector(variable="#" + ".".join(selector) + "#", value_selector=selector))
                 case "json" | "raw-text":
+                    if len(data) != 1:
+                        raise RequestBodyError("invalid body data, should have only one item")
                     selectors += variable_template_parser.extract_selectors_from_template(data[0].key)
                     selectors += variable_template_parser.extract_selectors_from_template(data[0].value)
                 case "x-www-form-urlencoded":