How to post change to Custom Field - DropDown Menu

1,675 views
Skip to first unread message

Luis N

unread,
Sep 4, 2018, 11:29:41 AM9/4/18
to NetBox
I can pull requests from netbox using curl in some of my bash scripts but when trying to post a change or update, it says 
  "detail": "Method \"POST\" not allowed."

Using Python 3.5.2 and Netbox 2.4.4 on Ubuntu

curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <TOKEN>" -H "Accept: application/json; indent=2" --data '{ "custom_fields":, "Requested":, "value": 6,  "label": Yes }' 'http://netbox01/api/dcim/devices/5/'

Jeremy Stretch

unread,
Sep 4, 2018, 11:36:13 AM9/4/18
to Luis N, NetBox
POST is for creating new objects, e.g. through /api/dcim/devices/.

Use PATCH for updating existing devices (e.g. /api/dcim/devices/5/).

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/9af13f4b-d234-47be-9edf-0207aff68352%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luis N

unread,
Sep 4, 2018, 11:47:24 AM9/4/18
to jstr...@digitalocean.com, netbox-...@googlegroups.com
Getting closer, I now get "detail": "JSON parse error - Expecting value: line 1 column 19 (char 18)"

On Tue, Sep 4, 2018 at 11:36 AM Jeremy Stretch <jstr...@digitalocean.com> wrote:
POST is for creating new objects, e.g. through /api/dcim/devices/.

Use PATCH for updating existing devices (e.g. /api/dcim/devices/5/).
On Tue, Sep 4, 2018 at 11:29 AM, Luis N <luis....@gmail.com> wrote:
I can pull requests from netbox using curl in some of my bash scripts but when trying to post a change or update, it says 
  "detail": "Method \"POST\" not allowed."

Using Python 3.5.2 and Netbox 2.4.4 on Ubuntu

curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <TOKEN>" -H "Accept: application/json; indent=2" --data '{ "custom_fields":, "Requested":, "value": 6,  "label": Yes }' 'http://netbox01/api/dcim/devices/5/'

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.

Luis N

unread,
Sep 4, 2018, 12:13:10 PM9/4/18
to jstr...@digitalocean.com, netbox-...@googlegroups.com
I was able to fix the format of the payload, now get this

The complete exception is provided below:

<class 'TypeError'>
int() argument must be a string, a bytes-like object or a number, not 'dict'

updated it to 

'{"custom_fields": {"Requested": {"value": "6","label": "Yes"}}}'

Luis N

unread,
Sep 5, 2018, 12:07:16 PM9/5/18
to Jeremy Stretch, netbox-...@googlegroups.com
Custom fields, can we even edit them via API ?? Anyone ? I am able to update serial #, etc not custom fields

Luis N

unread,
Sep 12, 2018, 1:55:02 PM9/12/18
to Jeremy Stretch, netbox-...@googlegroups.com
Really frustrating here that I cannot find a doc specifying how to change the custom field value for a selection via the API.

Amit

unread,
Sep 12, 2018, 3:09:06 PM9/12/18
to NetBox

I having update custom list field using pynetbox though those accept only int value of those list item which can only be obtained from html render page . 

something linke this in my code. 

_device_instance.custom_fields['dev_discovery_status'] = int(dev_discovery_status)

Amit

unread,
Sep 12, 2018, 3:25:52 PM9/12/18
to NetBox

curl -X  PATCH -H "Content-Type: application/json" -H "Authorization: Token <MYTOKEN >" -H "Accept: application/json; indent=2" --data '{ "custom_fields" : { "dev_discovery_status": 17 }}' http://MYNETBOX/api/dcim/devices/71/

This will work for you. 


curl -X POST -H "Content-Type: application/json" -H "Authorization: Token <TOKEN>" -H "Accept: application/json; indent=2" --data '{ "custom_fields": { "Requested": 6 }}' http://netbox01/api/dcim/devices/5/

Amit

unread,
Sep 12, 2018, 3:26:48 PM9/12/18
to NetBox
Corrected Typo

curl -X PATCH -H "Content-Type: application/json" -H "Authorization: Token <TOKEN>" -H "Accept: application/json; indent=2" --data '{ "custom_fields": { "Requested": 6 }}' http://netbox01/api/dcim/devices/5/

Igor Stojnov

unread,
Nov 12, 2018, 8:17:50 AM11/12/18
to NetBox

Hello all,

I have the same issue, trying to patch a device via API. Just as with you, it's only for custom fields, and for me it's happening for Choice custom fields.

The setup

NetBox: v2.4.3

Python: 2.7.5

OS: RHEL 7.5



Here's the JSON excerpt for changing those 2 custom field values:

{
"custom_fields":
  {
        "pci_device": {
            "value": 2,
            "label": "No"
        },
        "environment_tag": {
            "value": 7,
            "label": "PROD"
        },
  }
}


Or, transalated into CURL synthax:

curl -X PATCH "https://netbox.lab.local/api/dcim/devices/117/" -H  "accept: application/json" -H  "Content-Type: application/json" -H  "X-CSRFToken: c3zB2CUw4ElGLpfl7upPtG4VpKbB5qnq4" -d "{\"custom_fields\":  {        \"pci_device\": {            \"value\": 2,            \"label\": \"No\"        },        \"environment_tag\": {            \"value\": 7,            \"label\": \"PROD\"        },        \"tech_owner\": \"TDB\"  }}"


The error I get is:

<class 'TypeError'>
int() argument must be a string, a bytes-like object or a number, not 'dict'


What am I doing wrong?



Regards,
Igor

lamp...@gmail.com

unread,
Nov 12, 2018, 4:30:43 PM11/12/18
to NetBox
Don't send a dict of the value and the label, the API only needs to know the value, it cares nothing about the label when you are trying to change data.

So your payload should simply be:

{
"custom_fields":
  {
        "pci_device": 2,
        "environment_tag": 7,

Igor Stojnov

unread,
Nov 13, 2018, 2:23:21 AM11/13/18
to NetBox

Thank you kindly for the advice, it works! And so much simpler.

Regards,
Igor
Reply all
Reply to author
Forward
0 new messages