Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bdlamprecht opened this issue Jun 26, 2017 · 8 comments
Closed

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

bdlamprecht opened this issue Jun 26, 2017 · 8 comments
Labels
type: bug A confirmed report of unexpected behavior in the application

Comments

@bdlamprecht
Copy link
Contributor

bdlamprecht commented 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
}
@bdlamprecht bdlamprecht changed the title Adding objects with "custom_fields" does not work. Adding objects in API with "custom_fields" does not work. Jun 26, 2017
@jeremystretch
Copy link
Member

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?

@bdlamprecht
Copy link
Contributor Author

bdlamprecht commented Jun 28, 2017 via email

@jeremystretch
Copy link
Member

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").

@bdlamprecht
Copy link
Contributor Author

@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.

@jeremystretch
Copy link
Member

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.

@bdlamprecht
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

@jeremystretch
Copy link
Member

jeremystretch commented 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 jeremystretch added the type: bug A confirmed report of unexpected behavior in the application label Jun 30, 2017
lampwins pushed a commit to lampwins/netbox that referenced this issue Oct 13, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

2 participants