Error while serializing object #10552

Closed
opened 2025-12-29 21:33:03 +01:00 by adam · 2 comments
Owner

Originally created by @WagoL on GitHub (Dec 9, 2024).

Deployment Type

Self-hosted

Triage priority

N/A

NetBox Version

v4.1.7

Python Version

3.10

Steps to Reproduce

Create a webhook with the following settings.

{
    "id": 1,
    "url": "https://netbox.example.com/api/extras/webhooks/1/",
    "display_url": "https://netbox.example.com/extras/webhooks/1/",
    "display": "Hook-1",
    "name": "Hook-1",
    "description": "",
    "payload_url": "http://worker.example.com/event",
    "http_method": "POST",
    "http_content_type": "application/x-www-form-urlencoded",
    "additional_headers": "Authorization:aToken",
    "body_template": "{% set post_data = {\r\n    \"type\": \"TRIGGER_IF_SYNCMACDCIMTOXIQSE\",\r\n    \"area\": \"X\",\r\n    \"data\": {\r\n        \"event\": event,\r\n        \"username\": username,\r\n        \"model\": model,\r\n        \"timestamp\": timestamp,\r\n        \"data\": data,\r\n        \"snapshots\": snapshots\r\n    }\r\n} %}{{ post_data | tojson | urlencode }}",
    "secret": "",
    "ssl_verification": false,
    "ca_file_path": null,
    "custom_fields": {},
    "tags": [],
    "created": "2024-10-22T13:00:58.333909Z",
    "last_updated": "2024-12-09T15:39:35.443796Z"
}

Body template:

{% set post_data = {
    "type": "TRIGGER_IF_SYNCMACDCIMTOXIQSE",
    "area": "X",
    "data": {
        "event": event,
        "username": username,
        "model": model,
        "timestamp": timestamp,
        "data": data,
        "snapshots": snapshots
    }
} %}{{ post_data | tojson | urlencode }}

The webhook needs to send the data in the application/x-www-form-urlencoded format with some additional fields.

Expected Behavior

Expected the netbox object to be serialized and the webhook being sent out.

Observed Behavior

The webhook is not being sent and this error is observed in the rq-worker log.

Dec 09 16:39:54 netbox-server-1 python3[602216]: 15:39:54 default: extras.webhooks.send_webhook(data={'id': 145322, 'url': '/api/ipam/ip-addresses/145322/', 'display_url': '/ip..., event_r>
Dec 09 16:39:54 netbox-server-1 python3[602318]: 15:39:54 [Job 347a07fe-3722-4c8a-96fa-6213ca69a7ba]: exception raised while executing (extras.webhooks.send_webhook)
Dec 09 16:39:54 netbox-server-1 python3[602318]: Traceback (most recent call last):
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox/venv/lib/python3.10/site-packages/rq/worker.py", line 1573, in perform_job
Dec 09 16:39:54 netbox-server-1 python3[602318]:     rv = job.perform()
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 1322, in perform
Dec 09 16:39:54 netbox-server-1 python3[602318]:     self._result = self._execute()
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 1356, in _execute
Dec 09 16:39:54 netbox-server-1 python3[602318]:     result = self.func(*self.args, **self.kwargs)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox-4.1.7/netbox/extras/webhooks.py", line 60, in send_webhook
Dec 09 16:39:54 netbox-server-1 python3[602318]:     body = webhook.render_body(context)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox-4.1.7/netbox/extras/models/models.py", line 274, in render_body
Dec 09 16:39:54 netbox-server-1 python3[602318]:     return render_jinja2(self.body_template, context)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox-4.1.7/netbox/utilities/jinja2.py", line 53, in render_jinja2
Dec 09 16:39:54 netbox-server-1 python3[602318]:     return environment.from_string(source=template_code).render(**context)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 1304, in render
Dec 09 16:39:54 netbox-server-1 python3[602318]:     self.environment.handle_exception()
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 939, in handle_exception
Dec 09 16:39:54 netbox-server-1 python3[602318]:     raise rewrite_traceback_stack(source=source)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "<template>", line 12, in top-level template code
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/filters.py", line 1714, in do_tojson
Dec 09 16:39:54 netbox-server-1 python3[602318]:     return htmlsafe_json_dumps(value, dumps=dumps, **kwargs)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/utils.py", line 658, in htmlsafe_json_dumps
Dec 09 16:39:54 netbox-server-1 python3[602318]:     dumps(obj, **kwargs)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps
Dec 09 16:39:54 netbox-server-1 python3[602318]:     **kw).encode(obj)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
Dec 09 16:39:54 netbox-server-1 python3[602318]:     chunks = self.iterencode(o, _one_shot=True)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
Dec 09 16:39:54 netbox-server-1 python3[602318]:     return _iterencode(o, 0)
Dec 09 16:39:54 netbox-server-1 python3[602318]:   File "/usr/lib/python3.10/json/encoder.py", line 179, in default
Dec 09 16:39:54 netbox-server-1 python3[602318]:     raise TypeError(f'Object of type {o.__class__.__name__} '
Dec 09 16:39:54 netbox-server-1 python3[602318]: TypeError: Object of type __proxy__ is not JSON serializable
Originally created by @WagoL on GitHub (Dec 9, 2024). ### Deployment Type Self-hosted ### Triage priority N/A ### NetBox Version v4.1.7 ### Python Version 3.10 ### Steps to Reproduce Create a webhook with the following settings. ``` { "id": 1, "url": "https://netbox.example.com/api/extras/webhooks/1/", "display_url": "https://netbox.example.com/extras/webhooks/1/", "display": "Hook-1", "name": "Hook-1", "description": "", "payload_url": "http://worker.example.com/event", "http_method": "POST", "http_content_type": "application/x-www-form-urlencoded", "additional_headers": "Authorization:aToken", "body_template": "{% set post_data = {\r\n \"type\": \"TRIGGER_IF_SYNCMACDCIMTOXIQSE\",\r\n \"area\": \"X\",\r\n \"data\": {\r\n \"event\": event,\r\n \"username\": username,\r\n \"model\": model,\r\n \"timestamp\": timestamp,\r\n \"data\": data,\r\n \"snapshots\": snapshots\r\n }\r\n} %}{{ post_data | tojson | urlencode }}", "secret": "", "ssl_verification": false, "ca_file_path": null, "custom_fields": {}, "tags": [], "created": "2024-10-22T13:00:58.333909Z", "last_updated": "2024-12-09T15:39:35.443796Z" } ``` Body template: ``` {% set post_data = { "type": "TRIGGER_IF_SYNCMACDCIMTOXIQSE", "area": "X", "data": { "event": event, "username": username, "model": model, "timestamp": timestamp, "data": data, "snapshots": snapshots } } %}{{ post_data | tojson | urlencode }} ``` The webhook needs to send the data in the `application/x-www-form-urlencoded` format with some additional fields. ### Expected Behavior Expected the netbox object to be serialized and the webhook being sent out. ### Observed Behavior The webhook is not being sent and this error is observed in the rq-worker log. ``` Dec 09 16:39:54 netbox-server-1 python3[602216]: 15:39:54 default: extras.webhooks.send_webhook(data={'id': 145322, 'url': '/api/ipam/ip-addresses/145322/', 'display_url': '/ip..., event_r> Dec 09 16:39:54 netbox-server-1 python3[602318]: 15:39:54 [Job 347a07fe-3722-4c8a-96fa-6213ca69a7ba]: exception raised while executing (extras.webhooks.send_webhook) Dec 09 16:39:54 netbox-server-1 python3[602318]: Traceback (most recent call last): Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox/venv/lib/python3.10/site-packages/rq/worker.py", line 1573, in perform_job Dec 09 16:39:54 netbox-server-1 python3[602318]: rv = job.perform() Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 1322, in perform Dec 09 16:39:54 netbox-server-1 python3[602318]: self._result = self._execute() Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox/venv/lib/python3.10/site-packages/rq/job.py", line 1356, in _execute Dec 09 16:39:54 netbox-server-1 python3[602318]: result = self.func(*self.args, **self.kwargs) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox-4.1.7/netbox/extras/webhooks.py", line 60, in send_webhook Dec 09 16:39:54 netbox-server-1 python3[602318]: body = webhook.render_body(context) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox-4.1.7/netbox/extras/models/models.py", line 274, in render_body Dec 09 16:39:54 netbox-server-1 python3[602318]: return render_jinja2(self.body_template, context) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox-4.1.7/netbox/utilities/jinja2.py", line 53, in render_jinja2 Dec 09 16:39:54 netbox-server-1 python3[602318]: return environment.from_string(source=template_code).render(**context) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 1304, in render Dec 09 16:39:54 netbox-server-1 python3[602318]: self.environment.handle_exception() Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/environment.py", line 939, in handle_exception Dec 09 16:39:54 netbox-server-1 python3[602318]: raise rewrite_traceback_stack(source=source) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "<template>", line 12, in top-level template code Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/filters.py", line 1714, in do_tojson Dec 09 16:39:54 netbox-server-1 python3[602318]: return htmlsafe_json_dumps(value, dumps=dumps, **kwargs) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/data/netbox/venv/lib/python3.10/site-packages/jinja2/utils.py", line 658, in htmlsafe_json_dumps Dec 09 16:39:54 netbox-server-1 python3[602318]: dumps(obj, **kwargs) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps Dec 09 16:39:54 netbox-server-1 python3[602318]: **kw).encode(obj) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/usr/lib/python3.10/json/encoder.py", line 199, in encode Dec 09 16:39:54 netbox-server-1 python3[602318]: chunks = self.iterencode(o, _one_shot=True) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode Dec 09 16:39:54 netbox-server-1 python3[602318]: return _iterencode(o, 0) Dec 09 16:39:54 netbox-server-1 python3[602318]: File "/usr/lib/python3.10/json/encoder.py", line 179, in default Dec 09 16:39:54 netbox-server-1 python3[602318]: raise TypeError(f'Object of type {o.__class__.__name__} ' Dec 09 16:39:54 netbox-server-1 python3[602318]: TypeError: Object of type __proxy__ is not JSON serializable ```
adam added the type: bug label 2025-12-29 21:33:03 +01:00
adam closed this issue 2025-12-29 21:33:04 +01:00
Author
Owner

@bctiemann commented on GitHub (Dec 9, 2024):

Looks like this occurs as a result of the {{ post_data | tojson }} filter construct.

@bctiemann commented on GitHub (Dec 9, 2024): Looks like this occurs as a result of the `{{ post_data | tojson }}` filter construct.
Author
Owner

@jeremystretch commented on GitHub (Dec 27, 2024):

Certain objects within the data payload cannot be guaranteed to be serializable. In this case, it looks like lazy resolution is tripping up the JSON serializer. You'll need to investigate exactly which item within the context data is causing the issue and account for it. Unfortunately I don't think there's anything we can do for a blanket solution here, so I'm going to close this out.

@jeremystretch commented on GitHub (Dec 27, 2024): Certain objects within the `data` payload cannot be guaranteed to be serializable. In this case, it looks like lazy resolution is tripping up the JSON serializer. You'll need to investigate exactly which item within the context data is causing the issue and account for it. Unfortunately I don't think there's anything we can do for a blanket solution here, so I'm going to close this out.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10552