Skip to content

WebRTC offer fails with ContentTypeError 404 — camera.py does not handle non-JSON response from go2rtc #1069

@jfjeronimo

Description

@jfjeronimo

WebRTC offer fails with ContentTypeError 404 — camera.py does not handle non-JSON response from go2rtc

Version of the custom_component

5.15.2

Frigate version

0.17.1

Home Assistant version

Core 2026.3.2
aiohttp 3.13.3
Python 3.14

Configuration

Standard Frigate setup with go2rtc embedded (port 1984 internal, proxied via Frigate on port 5000).
Camera streams defined in go2rtc.streams using HTTP-FLV as primary source (Reolink cameras).

Describe the bug

When opening a camera in the Home Assistant frontend via WebRTC, the integration crashes with a ContentTypeError because camera.py unconditionally calls .json() on the response from the go2rtc WebRTC endpoint, without first checking the HTTP status code or the Content-Type header.

When go2rtc returns a 404 (e.g. stream not yet available or momentarily unavailable after a camera reconnection), the response body is text/plain; charset=utf-8 instead of application/json. The .json() call then raises an unhandled exception that propagates all the way up to the WebSocket API handler.

Error log

aiohttp.client_exceptions.ContentTypeError: 404, message='Attempt to decode JSON with unexpected mimetype: text/plain; charset=utf-8', url='http://localhost:5000/api/go2rtc/webrtc?src=camera_portero'

ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [139698956728608] Error handling message: Unknown error (unknown_error)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 28, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/camera/webrtc.py", line 234, in validate
    await func(connection, msg, camera)
  File "/usr/src/homeassistant/homeassistant/components/camera/webrtc.py", line 284, in ws_webrtc_offer
    await camera.async_handle_async_webrtc_offer(offer, session_id, send_message)
  File "/config/custom_components/frigate/camera.py", line 601, in async_handle_async_webrtc_offer
    answer = await resp.json()
             ^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/aiohttp_client.py", line 188, in json
    return await super().json(*args, loads=loads, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/site-packages/aiohttp/client_reqrep.py", line 756, in json
    raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 404, message='Attempt to decode JSON with unexpected mimetype: text/plain; charset=utf-8', url='http://localhost:5000/api/go2rtc/webrtc?src=camera_trasera'

Root cause

In custom_components/frigate/camera.py, line ~601:

answer = await resp.json()

This call is made without checking resp.status or resp.content_type first. When go2rtc returns a non-2xx response (in this case a 404 with a plain-text body), aiohttp raises ContentTypeError because the response is not valid JSON.

Expected behavior

The integration should check the HTTP status code before attempting to parse the response as JSON, and return a meaningful error to the frontend instead of an unhandled exception. Something like:

if resp.status != 200:
    _LOGGER.error("go2rtc WebRTC endpoint returned %s for stream %s", resp.status, src)
    send_message(websocket_api.error_message(msg["id"], "webrtc_offer_failed", f"go2rtc returned {resp.status}"))
    return
answer = await resp.json()

Additional context

  • The 404 from go2rtc occurs after a camera temporarily disconnects and go2rtc has not yet re-established the stream. The stream recovers on its own, but the unhandled exception in the integration prevents the frontend from showing a useful error and also generates noise in the HA logs.
  • This affects any camera using go2rtc streams via the Frigate integration when the underlying stream is transiently unavailable.
  • Reproducible consistently by temporarily disconnecting a camera from the network while the HA frontend has a WebRTC view open.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions