Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/prefect/blocks/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,12 @@ class CustomWebhookNotificationBlock(NotificationBlock):
' "{{tokenFromSecrets}}"}'
],
)
form_data: Optional[dict[str, str]] = Field(
form_data: Optional[dict[str, str] | str] = Field(
default=None,
title="Form Data",
description=(
"Send form data as payload. Should not be used together with _JSON Data_."
"Send form data as payload. Should not be used together with _JSON Data_. "
"Can be a dictionary for form-encoded data or a string for raw body content."
),
examples=[
'{"text": "{{subject}}\\n{{body}}", "title": "{{name}}", "token":'
Expand Down Expand Up @@ -882,13 +883,18 @@ def _build_request_args(self, body: str, subject: str | None) -> dict[str, Any]:
"name": self.name,
}
)
# httpx uses 'data' for form-encoded dicts, 'content' for raw string/bytes
if isinstance(self.form_data, str):
data_key = "content"
else:
data_key = "data"
# do substution
return apply_values(
{
"method": self.method,
"url": self.url,
"params": self.params,
"data": self.form_data,
data_key: self.form_data,
"json": self.json_data,
"headers": self.headers,
"cookies": self.cookies,
Expand Down
24 changes: 24 additions & 0 deletions tests/blocks/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,30 @@ def test_provide_both_data_and_json_raises_validation_error(self):
secrets={"token": "someSecretToken"},
)

async def test_string_form_data(self):
"""Test that form_data accepts a string for raw body content.

This enables forwarding pre-constructed JSON from automation bodies.
See: https://github.com/PrefectHQ/prefect/issues/19949
"""
with respx.mock(using="httpx") as xmock:
xmock.post("https://example.com/")

custom_block = CustomWebhookNotificationBlock(
name="test name",
url="https://example.com/",
form_data="{{body}}",
headers={"Content-Type": "application/json"},
)
# Simulate automation passing JSON as the body
await custom_block.notify(
'{"flow_name": "my-flow", "state": "Failed"}', "subject"
)

last_req = xmock.calls.last.request
assert last_req.content == b'{"flow_name": "my-flow", "state": "Failed"}'
assert last_req.headers["Content-Type"] == "application/json"


class TestSendgridEmail:
URL_PARAMS = {
Expand Down