Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f19aff8
Fix webhook exception when empty json data is sent
mikedast Nov 9, 2025
fba504c
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 10, 2025
1089488
fixed typo in comment.
mikedast Nov 10, 2025
b6cc17d
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 10, 2025
b9634e4
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 11, 2025
86d5ef2
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 12, 2025
0211e6f
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 12, 2025
93d5f71
use homeassistant.util.json.json_loads instread of json.loads. Also d…
mikedast Nov 13, 2025
c9a5e2f
re-add text check
mikedast Nov 13, 2025
925476e
Added test
mikedast Nov 15, 2025
95e69fe
renamed test
mikedast Nov 15, 2025
3fee39c
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 15, 2025
bbf2016
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 17, 2025
d0f9c65
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 18, 2025
f559a99
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 19, 2025
4572a34
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 19, 2025
00be452
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 25, 2025
25cac57
inlined code for better readability.
mikedast Nov 26, 2025
8f6b33d
Merge remote-tracking branch 'refs/remotes/origin/fix-webhook-empty-j…
mikedast Nov 26, 2025
a9b7cb5
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Nov 26, 2025
2a81109
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Dec 2, 2025
be25fa3
PR suggestion
mikedast Dec 3, 2025
ea014ec
Improved based on PR comments
mikedast Dec 3, 2025
f08d7b9
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Dec 3, 2025
4db2332
Merge branch 'dev' into fix-webhook-empty-json-exception
mikedast Dec 8, 2025
209a105
reverted exception handling
Dec 8, 2025
1f804ec
reverted exception handling
mikedast Dec 8, 2025
cbf2851
Merge remote-tracking branch 'refs/remotes/origin/fix-webhook-empty-j…
Dec 8, 2025
e5909e3
rebase
Dec 8, 2025
3eb6e43
Merge remote-tracking branch 'refs/remotes/origin/fix-webhook-empty-j…
mikedast Dec 8, 2025
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
5 changes: 4 additions & 1 deletion homeassistant/components/webhook/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from homeassistant.helpers.template import Template
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from homeassistant.util.json import json_loads

from . import (
DEFAULT_METHODS,
Expand Down Expand Up @@ -62,7 +63,9 @@ async def _handle_webhook(
base_result: dict[str, Any] = {"platform": "webhook", "webhook_id": webhook_id}

if "json" in request.headers.get(hdrs.CONTENT_TYPE, ""):
base_result["json"] = await request.json()
# Always attempt to read the body; request.text() returns "" if empty
text = await request.text()
base_result["json"] = json_loads(text) if text else {}
else:
base_result["data"] = await request.post()

Expand Down
44 changes: 44 additions & 0 deletions tests/components/webhook/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.setup import async_setup_component

from tests.common import async_capture_events
from tests.typing import ClientSessionGenerator


Expand Down Expand Up @@ -377,3 +378,46 @@ def store_event(event):

assert len(events) == 1
assert events[0].data["hello"] == "yo world"


async def test_webhook_query_json_header_no_payload(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> None:
"""Test requests with application/json header but no payload."""
events = async_capture_events(hass, "test_success")

assert await async_setup_component(
hass,
"automation",
{
"automation": {
"trigger": {
"platform": "webhook",
"webhook_id": "no_payload_webhook",
"local_only": True,
"allowed_methods": ["GET", "POST"],
},
"action": {
"event": "test_success",
},
}
},
)
await hass.async_block_till_done()
client = await hass_client_no_auth()

# GET
response = await client.get(
"/api/webhook/no_payload_webhook", headers={"Content-Type": "application/json"}
)
await hass.async_block_till_done()
assert response.status == 200

# POST
response = await client.post(
"/api/webhook/no_payload_webhook", headers={"Content-Type": "application/json"}
)
await hass.async_block_till_done()
assert response.status == 200

assert len(events) == 2