Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

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

-LAN-e068bbec737e
diff --git a/api/core/workflow/nodes/http_request/exc.py b/api/core/workflow/nodes/http_request/exc.py
index 7a5ab7dbc1c..a815f277bec 100644
--- a/api/core/workflow/nodes/http_request/exc.py
+++ b/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."""
diff --git a/api/core/workflow/nodes/http_request/executor.py b/api/core/workflow/nodes/http_request/executor.py
index 3b7e1933199..575db15d365 100644
--- a/api/core/workflow/nodes/http_request/executor.py
+++ b/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"
diff --git a/api/core/workflow/nodes/http_request/node.py b/api/core/workflow/nodes/http_request/node.py
index 171389a34c1..ebed690f6f3 100644
--- a/api/core/workflow/nodes/http_request/node.py
+++ b/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":