REST API: POST ​/dcim​/sites​/ Exception & Undocumented Mandatory Fields #2630

Closed
opened 2025-12-29 18:20:40 +01:00 by adam · 0 comments
Owner

Originally created by @heyglen on GitHub (May 23, 2019).

Environment

  • Python version: 3.6.8
  • NetBox version: 2.5.12

Steps to Reproduce

  1. Create a new DCIM Site object via the REST API.

According to the swagger docs, the required data is name & slug

HTTP POST https://netbox/api/dcim/sites/
HTTP Request Body
        {
            "name": "test",
            "slug": "test"
        }
  1. This returns a HTTP 500 error and throws the following exception in the log /var/log/netbox.log
Internal Server Error: /api/dcim/sites/
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/mixins.py", line 20, in create
    serializer.is_valid(raise_exception=True)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 236, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 434, in run_validation
    value = self.to_internal_value(data)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 488, in to_internal_value
    validated_value = field.run_validation(primitive_value)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/fields.py", line 536, in run_validation
    value = self.to_internal_value(data)
  File "/opt/netbox/netbox/extras/api/customfields.py", line 27, in to_internal_value
    for field_name, value in data.items():
AttributeError: 'str' object has no attribute 'items'
  1. If custom_fields is set to an empty dictionary, the above exception is no longer thrown. A HTTP 400 is returned informing of (undocumented) missing mandatory fields:
HTTP POST https://netbox/api/dcim/sites/
HTTP Request Body
        {
            "custom_fields": {},
            "name": "test",
            "slug": "test"
        }
HTTP Response 400 Body:
{
    "asn": [
        "A valid integer is required."
    ],
    "contact_email": [
        "Enter a valid email address."
    ],
    "latitude": [
        "A valid number is required."
    ],
    "longitude": [
        "A valid number is required."
    ],
    "status": [
        "None is not a valid choice."
    ],
    "tags": [
        "Invalid json list. A tag list submitted in string form must be valid json."
    ],
    "time_zone": [
        "Unknown time zone \"None\" (see pytz.common_timezones for all options)"
    ]
}

If these attributes are filled in as shown below, then the POST is accepted:

HTTP POST https://netbox/api/dcim/sites/
HTTP Request Body
        {
            "asn": "",
            "contact_email": "",
            "custom_fields": {},
            "latitude": "",
            "longitude": "",
            "name": "test",
            "slug": "test",
            "status": "",
            "tags": [],
            "time_zone": ""
        }
HTTP 201 Response Body:
        {
            "asn": null,
            "comments": "None",
            "contact_email": "",
            "contact_name": "None",
            "contact_phone": "None",
            "count_circuits": 0,
            "count_devices": 0,
            "count_prefixes": 0,
            "count_racks": 0,
            "count_vlans": 0,
            "created": "2019-05-23",
            "description": "None",
            "facility": "",
            "id": 8,
            "last_updated": "2019-05-23T12:01:41.814027Z",
            "latitude": null,
            "longitude": null,
            "name": "test",
            "physical_address": "None",
            "region": null,
            "shipping_address": "None",
            "slug": "test",
            "status": {
                "label": "Active",
                "value": 1
            },
            "tags": [],
            "tenant": null,
            "time_zone": null
        }

Expected Behavior

  • An HTTP POST to /api/dcim​/sites​/ with only the name & slug fields should succeed.

Observed Behavior

  • Exception thrown when the optional field custom_fields is not specified
  • The undocumented mandatory fields custom_fields, asn, contact_email, latitude, longitude, status, tags, time_zone are required
  • The fields asn & status are integer fields and should not be initialized with strings
Originally created by @heyglen on GitHub (May 23, 2019). <!-- NOTE: This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.6.8 * NetBox version: 2.5.12 <!-- Describe in detail the exact steps that someone else can take to reproduce this bug using the current stable release of NetBox (or the current beta release where applicable). Begin with the creation of any necessary database objects and call out every operation being performed explicitly. If reporting a bug in the REST API, be sure to reconstruct the raw HTTP request(s) being made: Don't rely on a wrapper like pynetbox. --> ### Steps to Reproduce 1. Create a new *DCIM Site* object via the REST API. According to the swagger docs, the required data is ```name``` & ```slug``` ``` HTTP POST https://netbox/api/dcim/sites/ HTTP Request Body { "name": "test", "slug": "test" } ``` 2. This returns a HTTP 500 error and throws the following exception in the log ```/var/log/netbox.log``` ``` Internal Server Error: /api/dcim/sites/ Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/usr/local/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view return self.dispatch(request, *args, **kwargs) File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch response = self.handle_exception(exc) File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception self.raise_uncaught_exception(exc) File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch response = handler(request, *args, **kwargs) File "/usr/local/lib/python3.6/site-packages/rest_framework/mixins.py", line 20, in create serializer.is_valid(raise_exception=True) File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 236, in is_valid self._validated_data = self.run_validation(self.initial_data) File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 434, in run_validation value = self.to_internal_value(data) File "/usr/local/lib/python3.6/site-packages/rest_framework/serializers.py", line 488, in to_internal_value validated_value = field.run_validation(primitive_value) File "/usr/local/lib/python3.6/site-packages/rest_framework/fields.py", line 536, in run_validation value = self.to_internal_value(data) File "/opt/netbox/netbox/extras/api/customfields.py", line 27, in to_internal_value for field_name, value in data.items(): AttributeError: 'str' object has no attribute 'items' ``` 3. If ```custom_fields``` is set to an empty dictionary, the above exception is no longer thrown. A HTTP 400 is returned informing of (undocumented) missing mandatory fields: ``` HTTP POST https://netbox/api/dcim/sites/ HTTP Request Body { "custom_fields": {}, "name": "test", "slug": "test" } HTTP Response 400 Body: { "asn": [ "A valid integer is required." ], "contact_email": [ "Enter a valid email address." ], "latitude": [ "A valid number is required." ], "longitude": [ "A valid number is required." ], "status": [ "None is not a valid choice." ], "tags": [ "Invalid json list. A tag list submitted in string form must be valid json." ], "time_zone": [ "Unknown time zone \"None\" (see pytz.common_timezones for all options)" ] } ``` If these attributes are filled in as shown below, then the POST is accepted: ``` HTTP POST https://netbox/api/dcim/sites/ HTTP Request Body { "asn": "", "contact_email": "", "custom_fields": {}, "latitude": "", "longitude": "", "name": "test", "slug": "test", "status": "", "tags": [], "time_zone": "" } HTTP 201 Response Body: { "asn": null, "comments": "None", "contact_email": "", "contact_name": "None", "contact_phone": "None", "count_circuits": 0, "count_devices": 0, "count_prefixes": 0, "count_racks": 0, "count_vlans": 0, "created": "2019-05-23", "description": "None", "facility": "", "id": 8, "last_updated": "2019-05-23T12:01:41.814027Z", "latitude": null, "longitude": null, "name": "test", "physical_address": "None", "region": null, "shipping_address": "None", "slug": "test", "status": { "label": "Active", "value": 1 }, "tags": [], "tenant": null, "time_zone": null } ``` <!-- What did you expect to happen? --> ### Expected Behavior * An HTTP POST to /api/dcim​/sites​/ with only the ```name``` & ```slug``` fields should succeed. <!-- What happened instead? --> ### Observed Behavior * Exception thrown when the optional field ```custom_fields``` is not specified * The undocumented mandatory fields ```custom_fields```, ```asn```, ```contact_email```, ```latitude```, ```longitude```, ```status```, ```tags```, ```time_zone``` are required * The fields ```asn``` & ```status``` are integer fields and should not be initialized with strings
adam closed this issue 2025-12-29 18:20:40 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2630