Adding objects in API with "custom_fields" does not work. #1057

Closed
opened 2025-12-29 16:28:23 +01:00 by adam · 8 comments
Owner

Originally created by @bdlamprecht on GitHub (Jun 26, 2017).

Issue type:

Bug

Python version:
2.7.12

NetBox version:
2.0.6

I can use the API to create new sites just fine (via the Swagger UI), but when I try to add a new site with a previously defined custom_field (via the admin panel in the WebUI), I get the following error: JSON parse error - Extra data: line 18 column 1 - line 18 column 2 (char 343 - 344)"

This works:

{
    "name": "Jacksonville-FL",
    "slug": "jax",
    "region": "1",
    "tenant": null,
    "facility": "",
    "asn": null,
    "physical_address": "",
    "shipping_address": "",
    "contact_name": "",
    "contact_phone": "",
    "contact_email": "",
    "comments": ""
}

This does not work:

{
    "name": "Jacksonville-FL",
    "slug": "jax",
    "region": "1",
    "tenant": null,
    "facility": "",
    "asn": null,
    "physical_address": "",
    "shipping_address": "",
    "contact_name": "",
    "contact_phone": "",
    "contact_email": "",
    "comments": "",
    "custom_fields": {
        "ce_qos_type": "2"
        }
   }
}

The error that I receive is as follows (from Swagger):

Curl
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-CSRFToken: [REMOVED]' -d '{ \ 
     "name": "Jacksonville-FL", \ 
     "slug": "jax", \ 
     "region": "1", \ 
     "tenant": null, \ 
     "facility": "", \ 
     "asn": null, \ 
     "physical_address": "", \ 
     "shipping_address": "", \ 
     "contact_name": "", \ 
     "contact_phone": "", \ 
     "contact_email": "", \ 
     "comments": "", \ 
     "custom_fields": { \ 
         "ce_qos_type": "2" \ 
         } \ 
    } \ 
 }' 'http://[HOSTNAME]/api/dcim/sites/'

Request URL
http://[HOSTNAME]/api/dcim/sites/

Response Body
{
  "detail": "JSON parse error - Extra data: line 18 column 1 - line 18 column 2 (char 343 - 344)"
}

Response Code
400

I've used the GET function to ensure that the custom_field is a correct value. For example:

curl -X GET --header 'Accept: application/json' --header 'X-CSRFToken: [REMOVED]' '[HOSTNAME]/api/dcim/sites/10/'

{
  "id": 10,
  "name": "Poughkeepsie-NY",
  "slug": "pok",
  "region": {
    "id": 1,
    "url": "[HOSTNAME]/api/dcim/regions/1/",
    "name": "[REMOVED]",
    "slug": "[REMOVED"
  },
  "tenant": null,
  "facility": "",
  "asn": null,
  "physical_address": "",
  "shipping_address": "",
  "contact_name": "",
  "contact_phone": "",
  "contact_email": "",
  "comments": "",
  "custom_fields": {
    "ce_qos_type": {
      "value": 2,
      "label": "6-cos"
    }
  },
  "count_prefixes": 0,
  "count_vlans": 0,
  "count_racks": 0,
  "count_devices": 0,
  "count_circuits": 0
}
Originally created by @bdlamprecht on GitHub (Jun 26, 2017). ### Issue type: Bug **Python version:** 2.7.12 **NetBox version:** 2.0.6 I can use the API to create new sites just fine (via the `Swagger UI`), but when I try to add a new site with a previously defined `custom_field` (via the admin panel in the WebUI), I get the following error: `JSON parse error - Extra data: line 18 column 1 - line 18 column 2 (char 343 - 344)"` This works: ``` { "name": "Jacksonville-FL", "slug": "jax", "region": "1", "tenant": null, "facility": "", "asn": null, "physical_address": "", "shipping_address": "", "contact_name": "", "contact_phone": "", "contact_email": "", "comments": "" } ``` This does not work: ``` { "name": "Jacksonville-FL", "slug": "jax", "region": "1", "tenant": null, "facility": "", "asn": null, "physical_address": "", "shipping_address": "", "contact_name": "", "contact_phone": "", "contact_email": "", "comments": "", "custom_fields": { "ce_qos_type": "2" } } } ``` The error that I receive is as follows (from `Swagger`): ``` Curl curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-CSRFToken: [REMOVED]' -d '{ \ "name": "Jacksonville-FL", \ "slug": "jax", \ "region": "1", \ "tenant": null, \ "facility": "", \ "asn": null, \ "physical_address": "", \ "shipping_address": "", \ "contact_name": "", \ "contact_phone": "", \ "contact_email": "", \ "comments": "", \ "custom_fields": { \ "ce_qos_type": "2" \ } \ } \ }' 'http://[HOSTNAME]/api/dcim/sites/' Request URL http://[HOSTNAME]/api/dcim/sites/ Response Body { "detail": "JSON parse error - Extra data: line 18 column 1 - line 18 column 2 (char 343 - 344)" } Response Code 400 ``` I've used the `GET` function to ensure that the `custom_field` is a correct value. For example: ``` curl -X GET --header 'Accept: application/json' --header 'X-CSRFToken: [REMOVED]' '[HOSTNAME]/api/dcim/sites/10/' { "id": 10, "name": "Poughkeepsie-NY", "slug": "pok", "region": { "id": 1, "url": "[HOSTNAME]/api/dcim/regions/1/", "name": "[REMOVED]", "slug": "[REMOVED" }, "tenant": null, "facility": "", "asn": null, "physical_address": "", "shipping_address": "", "contact_name": "", "contact_phone": "", "contact_email": "", "comments": "", "custom_fields": { "ce_qos_type": { "value": 2, "label": "6-cos" } }, "count_prefixes": 0, "count_vlans": 0, "count_racks": 0, "count_devices": 0, "count_circuits": 0 }
adam added the type: bug label 2025-12-29 16:28:23 +01:00
adam closed this issue 2025-12-29 16:28:23 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jun 28, 2017):

It looks like this might be an issue with the Swagger UI. When POSTing your example data with curl it works as expected:

curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <token>" -H "Accept: application/json; indent=4" http://localhost:8000/api/dcim/sites/ -d '{
    "name": "Jacksonville-FL",
    "slug": "jax",
    "region": "1",
    "tenant": null,
    "facility": "",
    "asn": null,
    "physical_address": "",
    "shipping_address": "",
    "contact_name": "",
    "contact_phone": "",
    "contact_email": "",
    "comments": "",
    "custom_fields": {
        "ce_qos_type": "2"
        }
}'

Just to confirm, you created the custom field as an integer field, correct?

@jeremystretch commented on GitHub (Jun 28, 2017): It looks like this might be an issue with the Swagger UI. When POSTing your example data with curl it works as expected: ``` curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <token>" -H "Accept: application/json; indent=4" http://localhost:8000/api/dcim/sites/ -d '{ "name": "Jacksonville-FL", "slug": "jax", "region": "1", "tenant": null, "facility": "", "asn": null, "physical_address": "", "shipping_address": "", "contact_name": "", "contact_phone": "", "contact_email": "", "comments": "", "custom_fields": { "ce_qos_type": "2" } }' ``` Just to confirm, you created the custom field as an integer field, correct?
Author
Owner

@bdlamprecht commented on GitHub (Jun 28, 2017):

No, it was created as a "Selection" field.

On Wed, Jun 28, 2017 at 11:25 AM, Jeremy Stretch notifications@github.com
wrote:

It looks like this might be an issue with the Swagger UI. When POSTing
your example data with curl it works as expected:

curl -X POST -H "Content-Type: application/json" -H "Authorization: Token " -H "Accept: application/json; indent=4" http://localhost:8000/api/dcim/sites/ -d '{
"name": "Jacksonville-FL",
"slug": "jax",
"region": "1",
"tenant": null,
"facility": "",
"asn": null,
"physical_address": "",
"shipping_address": "",
"contact_name": "",
"contact_phone": "",
"contact_email": "",
"comments": "",
"custom_fields": {
"ce_qos_type": "2"
}
}'

Just to confirm, you created the custom field as an integer field, correct?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/digitalocean/netbox/issues/1297#issuecomment-311729783,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHbDimcibzt_fCbuVCvx-qAPr_ckkd2cks5sIoxsgaJpZM4OFtsc
.

@bdlamprecht commented on GitHub (Jun 28, 2017): No, it was created as a "Selection" field. On Wed, Jun 28, 2017 at 11:25 AM, Jeremy Stretch <notifications@github.com> wrote: > It looks like this might be an issue with the Swagger UI. When POSTing > your example data with curl it works as expected: > > curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <token>" -H "Accept: application/json; indent=4" http://localhost:8000/api/dcim/sites/ -d '{ > "name": "Jacksonville-FL", > "slug": "jax", > "region": "1", > "tenant": null, > "facility": "", > "asn": null, > "physical_address": "", > "shipping_address": "", > "contact_name": "", > "contact_phone": "", > "contact_email": "", > "comments": "", > "custom_fields": { > "ce_qos_type": "2" > } > }' > > Just to confirm, you created the custom field as an integer field, correct? > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/digitalocean/netbox/issues/1297#issuecomment-311729783>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AHbDimcibzt_fCbuVCvx-qAPr_ckkd2cks5sIoxsgaJpZM4OFtsc> > . >
Author
Owner

@jeremystretch commented on GitHub (Jun 28, 2017):

POSTing the following works for me with a selection field:

"custom_fields": {
        "ce_qos_type": 6    
        }
}

Note that the value of the field is being passed as an integer. Passing the value as a string yields a validation error ("Invalid choice for field ce_qos_type: 6").

@jeremystretch commented on GitHub (Jun 28, 2017): POSTing the following works for me with a selection field: ``` "custom_fields": { "ce_qos_type": 6 } } ``` Note that the value of the field is being passed as an integer. Passing the value as a string yields a validation error ("Invalid choice for field ce_qos_type: 6").
Author
Owner

@bdlamprecht commented on GitHub (Jun 28, 2017):

@jeremystretch

I'm trying to get a string value to be stored in the dictionary "ce_qos_type" with one of two values.

Either:

  1. "4_cos" (the default)
  2. "6_cos"

I'm going to be using this with Ansible to setup QoS on our border devices, depending on whether the CE can handle 4 classes or 6 classes.

Perhaps I'm making this too complicated, if so, I'm open to suggestions.

@bdlamprecht commented on GitHub (Jun 28, 2017): @jeremystretch I'm trying to get a string value to be stored in the dictionary "ce_qos_type" with one of two values. Either: 1. "4_cos" (the default) 2. "6_cos" I'm going to be using this with Ansible to setup QoS on our border devices, depending on whether the CE can handle `4 classes` or `6 classes`. Perhaps I'm making this too complicated, if so, I'm open to suggestions.
Author
Owner

@jeremystretch commented on GitHub (Jun 28, 2017):

Selection fields have pre-defined values to choose from, created under the admin UI. You need to POST the primary key of the desired choice.

@jeremystretch commented on GitHub (Jun 28, 2017): Selection fields have pre-defined values to choose from, created under the admin UI. You need to POST the primary key of the desired choice.
Author
Owner

@bdlamprecht commented on GitHub (Jun 28, 2017):

The value of "1" is the primary key for "4_cos" above and the value of "2" is the primary key for "6_cos". This wasn't working in Swagger, and as you stated, it could be an error there, I'm not able to check today.

I'm a newbie on APIs, so tomorrow I'll investigate how to get a token in the CLI without using Swagger and report back.

Thanks for looking into this so quickly.

@bdlamprecht commented on GitHub (Jun 28, 2017): The value of "1" is the primary key for "4_cos" above and the value of "2" is the primary key for "6_cos". This wasn't working in Swagger, and as you stated, it could be an error there, I'm not able to check today. I'm a newbie on APIs, so tomorrow I'll investigate how to get a token in the CLI without using Swagger and report back. Thanks for looking into this so quickly.
Author
Owner

@bdlamprecht commented on GitHub (Jun 29, 2017):

Well, I was able to test it and I CAN get it to work, although I don't understand an error I'm encountering.

I created a key for myself and used this on the CLI:

 curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <token>" -H "Accept: application/json; indent=4" http://[HOSTNAME]/api/dcim/sites/ -d '{
    "name": "Jacksonville-FL",
    "slug": "jax",
    "region": "1",
    "tenant": null,
    "facility": "",
    "asn": null,
    "physical_address": "",
    "shipping_address": "",
    "contact_name": "",
    "contact_phone": "",
    "contact_email": "",
    "comments": "",
    "custom_fields": {
        "ce_qos_type": "2"
        }
}'

Which resulted in this error coming back:

{
    "custom_fields": [
        "Invalid choice for field ce_qos_type: 2"
    ]
}

So, I decided to try without the quotes around the value of 2 for the ce_qos_type and it WORKED. What I don't understand is, why does the primary key for region work with quotes, but this custom_field does not? I've triple-checked that all of my quotes have matching pairs and I'm not sure what else I'm missing.

Anyways, since I've got this to work, I'm happy to close this issue and I'm very appreciative for you walking me through this, however, I'm still curious about the error. Let me know how you prefer me to proceed.

@bdlamprecht commented on GitHub (Jun 29, 2017): Well, I was able to test it and I **CAN** get it to work, although I don't understand an error I'm encountering. I created a key for myself and used this on the CLI: ``` curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <token>" -H "Accept: application/json; indent=4" http://[HOSTNAME]/api/dcim/sites/ -d '{ "name": "Jacksonville-FL", "slug": "jax", "region": "1", "tenant": null, "facility": "", "asn": null, "physical_address": "", "shipping_address": "", "contact_name": "", "contact_phone": "", "contact_email": "", "comments": "", "custom_fields": { "ce_qos_type": "2" } }' ``` Which resulted in this error coming back: ``` { "custom_fields": [ "Invalid choice for field ce_qos_type: 2" ] } ``` So, I decided to try without the quotes around the value of `2` for the `ce_qos_type` and it **WORKED**. What I don't understand is, why does the primary key for `region` work with quotes, but this `custom_field` does not? I've triple-checked that all of my quotes have matching pairs and I'm not sure what else I'm missing. Anyways, since I've got this to work, I'm happy to close this issue and I'm very appreciative for you walking me through this, however, I'm still curious about the error. Let me know how you prefer me to proceed.
Author
Owner

@jeremystretch commented on GitHub (Jun 29, 2017):

What I don't understand is, why does the primary key for region work with quotes, but this custom_field does not?

Custom fields are a bit odd because we're serializing the value of the field to emulate different data types (e.g. string, integer, or boolean). This inconsistency is likely due to enforcement of the integer type for select fields, if that makes sense. But to your point, NetBox should accept a number-as-a-string for custom fields if other primary key fields do. I'll have to look into it some more.

@jeremystretch commented on GitHub (Jun 29, 2017): > What I don't understand is, why does the primary key for region work with quotes, but this custom_field does not? Custom fields are a bit odd because we're serializing the value of the field to emulate different data types (e.g. string, integer, or boolean). This inconsistency is likely due to enforcement of the integer type for select fields, if that makes sense. But to your point, NetBox should accept a number-as-a-string for custom fields if other primary key fields do. I'll have to look into it some more.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1057