500 while patching custom_field #1714

Closed
opened 2025-12-29 16:34:40 +01:00 by adam · 3 comments
Owner

Originally created by @alf632 on GitHub (May 3, 2018).

Issue type

[ ] Feature request
[x ] Bug report
[ ] Documentation

Environment

  • Python version: 3.5.3
  • NetBox version: 2.3.3

Description

Hi,
I encounter a similar Issue like #1648. In my case I'm trying to patch a custom_field of a virtual-machine.
What I am trying to achieve is to have my provisioning system update its status to the respective Object in Netbox.

requests.patch("http://netbox/api/virtualization/virtual-machines/1/", data={"custom_fields": {"install_status": "loaded into installserver"}}, headers={'Authorization': "Token " + token})

This is responded with a 500 with the following in the server log:

Internal Server Error: /api/virtualization/virtual-machines/1/
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/viewsets.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 84, in partial_update
    return self.update(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 69, in update
    serializer.is_valid(raise_exception=True)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 236, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 434, in run_validation
    value = self.to_internal_value(data)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 488, in to_internal_value
    validated_value = field.run_validation(primitive_value)
  File "/usr/local/lib/python3.5/dist-packages/rest_framework/fields.py", line 523, in run_validation
    value = self.to_internal_value(data)
  File "/opt/netbox-2.3.3/netbox/extras/api/customfields.py", line 29, in to_internal_value
    for field_name, value in data.items():
AttributeError: 'str' object has no attribute 'items'
[03/May/2018 13:02:34] "PATCH /api/virtualization/virtual-machines/1/ HTTP/1.1" 500 20735

A print(data) statement in netbox/extras/api/customfields.py shows "install_status" (similar to observations of @4ndersson in formerly mentioned issue, though i did not get his fix).

Following Screenshot shows the custom_fields configuration:
custom_fields
And this is the Object I'm trying to patch:

{
    "id": 1,
    "name": "test",
    "status": {
        "value": 1,
        "label": "Active"
    },
    "cluster": {
        "id": 1,
        "url": "http://netbox/api/virtualization/clusters/1/",
        "name": "Test-Proxmox"
    },
    "role": {
        "id": 1,
        "url": "http://netbox/api/dcim/device-roles/1/",
        "name": "server",
        "slug": "server"
    },
    "tenant": {
        "id": 1,
        "url": "http://netbox/api/tenancy/tenants/1/",
        "name": "tenant",
        "slug": "tenant"
    },
    "platform": null,
    "primary_ip": null,
    "primary_ip4": null,
    "primary_ip6": null,
    "vcpus": 1,
    "memory": 512,
    "disk": 32,
    "comments": "bla",
    "custom_fields": {
        "install_status": "bla",
        "install": false,
        "create_vm": false
    },
    "created": "2018-05-03",
    "last_updated": "2018-05-03T11:58:54.445805Z"
}

I tried to change the Content-Type Header to 'application/json' which was answered with a 400 and a less informative log entry:
[03/May/2018 14:44:38] "PATCH /api/virtualization/virtual-machines/1/ HTTP/1.1" 400 73

Originally created by @alf632 on GitHub (May 3, 2018). ### Issue type [ ] Feature request <!-- An enhancement of existing functionality --> [x ] Bug report <!-- Unexpected or erroneous behavior --> [ ] Documentation <!-- A modification to the documentation --> <!-- Please describe the environment in which you are running NetBox. (Be sure to verify that you are running the latest stable release of NetBox before submitting a bug report.) If you are submitting a bug report and have made any changes to the code base, please first validate that your bug can be recreated while running an official release. --> ### Environment * Python version: 3.5.3 <!-- Example: 3.5.4 --> * NetBox version: 2.3.3 <!-- Example: 2.1.3 --> <!-- BUG REPORTS must include: * A list of the steps needed for someone else to reproduce the bug * A description of the expected and observed behavior * Any relevant error messages (screenshots may also help) FEATURE REQUESTS must include: * A detailed description of the proposed functionality * A use case for the new feature * A rough description of any necessary changes to the database schema * Any relevant third-party libraries which would be needed --> ### Description Hi, I encounter a similar Issue like #1648. In my case I'm trying to patch a custom_field of a virtual-machine. What I am trying to achieve is to have my provisioning system update its status to the respective Object in Netbox. `requests.patch("http://netbox/api/virtualization/virtual-machines/1/", data={"custom_fields": {"install_status": "loaded into installserver"}}, headers={'Authorization': "Token " + token})` This is responded with a 500 with the following in the server log: ``` Internal Server Error: /api/virtualization/virtual-machines/1/ Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/rest_framework/viewsets.py", line 103, in view return self.dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 483, in dispatch response = self.handle_exception(exc) File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 443, in handle_exception self.raise_uncaught_exception(exc) File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 480, in dispatch response = handler(request, *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 84, in partial_update return self.update(request, *args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 69, in update serializer.is_valid(raise_exception=True) File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 236, in is_valid self._validated_data = self.run_validation(self.initial_data) File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 434, in run_validation value = self.to_internal_value(data) File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 488, in to_internal_value validated_value = field.run_validation(primitive_value) File "/usr/local/lib/python3.5/dist-packages/rest_framework/fields.py", line 523, in run_validation value = self.to_internal_value(data) File "/opt/netbox-2.3.3/netbox/extras/api/customfields.py", line 29, in to_internal_value for field_name, value in data.items(): AttributeError: 'str' object has no attribute 'items' [03/May/2018 13:02:34] "PATCH /api/virtualization/virtual-machines/1/ HTTP/1.1" 500 20735 ``` A print(data) statement in netbox/extras/api/customfields.py shows "install_status" (similar to observations of @4ndersson in formerly mentioned issue, though i did not get his fix). Following Screenshot shows the custom_fields configuration: ![custom_fields](https://user-images.githubusercontent.com/3483346/39578752-4a5b55b4-4ee5-11e8-9505-ed9588526b69.png) And this is the Object I'm trying to patch: ``` { "id": 1, "name": "test", "status": { "value": 1, "label": "Active" }, "cluster": { "id": 1, "url": "http://netbox/api/virtualization/clusters/1/", "name": "Test-Proxmox" }, "role": { "id": 1, "url": "http://netbox/api/dcim/device-roles/1/", "name": "server", "slug": "server" }, "tenant": { "id": 1, "url": "http://netbox/api/tenancy/tenants/1/", "name": "tenant", "slug": "tenant" }, "platform": null, "primary_ip": null, "primary_ip4": null, "primary_ip6": null, "vcpus": 1, "memory": 512, "disk": 32, "comments": "bla", "custom_fields": { "install_status": "bla", "install": false, "create_vm": false }, "created": "2018-05-03", "last_updated": "2018-05-03T11:58:54.445805Z" } ``` I tried to change the Content-Type Header to 'application/json' which was answered with a 400 and a less informative log entry: [03/May/2018 14:44:38] "PATCH /api/virtualization/virtual-machines/1/ HTTP/1.1" 400 73
adam closed this issue 2025-12-29 16:34:40 +01:00
Author
Owner

@bartdebruijn commented on GitHub (May 29, 2018):

I run into something similar, unfortunately. But only with custom fields.

@bartdebruijn commented on GitHub (May 29, 2018): I run into something similar, unfortunately. But only with custom fields.
Author
Owner

@bartdebruijn commented on GitHub (May 29, 2018):

@alf632

I found that this has to do with how the requests module parses the nested json. Basically, you need to use the "json" parameter for this to work, instead of "data". See here for more information: https://stackoverflow.com/questions/9733638/post-json-using-python-requests/26344315#26344315

This fixed the issue for me.

Kind regards,

@bartdebruijn commented on GitHub (May 29, 2018): @alf632 I found that this has to do with how the requests module parses the nested json. Basically, you need to use the "json" parameter for this to work, instead of "data". See here for more information: https://stackoverflow.com/questions/9733638/post-json-using-python-requests/26344315#26344315 This fixed the issue for me. Kind regards,
Author
Owner

@alf632 commented on GitHub (May 29, 2018):

@bartdebruijn Thx mate! This works for me, too.

Now one only has to remember:

  • to end the URL with a slash else:
    RuntimeError: You called this URL via PATCH, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining PATCH data. Change your form to point to netbox:8000/api/virtualization/virtual-machines/3/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

  • to add all mandatory fields else:
    >>> r.json()
    {u'custom_fields': [u'Missing required fields: ... ']}`

  • to change those mandatory fields from:
    {u'custom_fields': {u'kernel': {u'value': 1, u'label': u'Stable'}, ... }}
    to
    {u'custom_fields': {u'kernel': 1, ... }}
    if they are of type "Selection" or else:
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'

@alf632 commented on GitHub (May 29, 2018): @bartdebruijn Thx mate! This works for me, too. Now one only has to remember: - to end the URL with a slash else: `RuntimeError: You called this URL via PATCH, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining PATCH data. Change your form to point to netbox:8000/api/virtualization/virtual-machines/3/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.` - to add all mandatory fields else: `>>> r.json()` `{u'custom_fields':` [u'Missing required fields: ... ']}` - to change those mandatory fields from: `{u'custom_fields': {u'kernel': {u'value': 1, u'label': u'Stable'}, ... }}` to `{u'custom_fields': {u'kernel': 1, ... }}` if they are of type "Selection" or else: `TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1714