Rest API to set custom field #13425
Answered
by
mtinberg
andnym-tt
asked this question in
Help Wanted!
Replies: 1 comment
-
When doing a PATCH for a reference field you just need an array of the ID numbers you want to reference, eg.
{
"custom_fields": {
"ApplicationNodes": [211, 212]
}
}
The format you read the data is for ease of use and in is not exactly the format you write the data in, kind of like the CSV support in that way.
—
Mark Tinberg ***@***.***>
Division of Information Technology-Network Services
University of Wisconsin-Madison
…________________________________
From: andnym-tt ***@***.***>
Sent: Wednesday, August 9, 2023 9:21 AM
To: netbox-community/netbox ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [netbox-community/netbox] Rest API to set custom field (Discussion #13425)
I've fairly new to Netbox Rest API. I'm trying to investigate if I can update a Netbox object (virtualization cluster) that has a custom object that is a multi object type. I found an earlier discussion regarding this issue but it has been resolved. So I'm not sure if I'm doing anything wrong or if I found a situation where the issue still exist?
It looks like this when I read it out
"custom_fields": {
"ApplicationNodes": [
{
"id": 211,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/211/",
"display": "testnode1",
"name": "testnode1"
},
{
"id": 212,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/212/",
"display": "testnode2",
"name": "testnode2"
}
],
"ClusterManager": null,
"MasterClusterID": null
},
I have succeded in clear the ApplicationNodes custom object with this command:
curl -X 'PATCH'
'https://10.166.168.199/api/virtualization/clusters/19/'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-H 'X-CSRFTOKEN: yIYXUSHeuIjAbBDMQKkSCzxHQO9BaELwuNr5MGurtP4HqdPcKMRRT56MQLKw1Avm'
-d '{
"custom_fields": {
"ApplicationNodes":null
}
}'
Verify by read again:
"custom_fields": {
"ApplicationNodes": null,
"ClusterManager": null,
"MasterClusterID": null
},
Then I try to put the information back:
curl -X 'PATCH'
'https://10.166.168.199/api/virtualization/clusters/19/'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-H 'X-CSRFTOKEN: yIYXUSHeuIjAbBDMQKkSCzxHQO9BaELwuNr5MGurtP4HqdPcKMRRT56MQLKw1Avm'
-d '{
"custom_fields": {
"ApplicationNodes": [
{
"id": 211,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/211/",
"display": "testnode1",
"name": "testnode1"
},
{
"id": 212,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/212/",
"display": "testnode2",
"name": "testnode2"
}
]
}
}'
I get code 400 Bad Request:
{
"custom_fields": [
"Unknown related object(s): [{'id': 211, 'url': 'https://10.166.168.199/api/virtualization/virtual-machines/211/', 'display': 'testnode1', 'name': 'testnode1'}, {'id': 212, 'url': 'https://10.166.168.199/api/virtualization/virtual-machines/212/', 'display': 'testnode2', 'name': 'testnode2'}]"
]
}
I'm running
Netbox version 3.5.3
Python version 3.8.16
—
Reply to this email directly, view it on GitHub<#13425>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAS7UM7PMNDAX4ZCWCTPWV3XUOMFFANCNFSM6AAAAAA3KCXKVE>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
andnym-tt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've fairly new to Netbox Rest API. I'm trying to investigate if I can update a Netbox object (virtualization cluster) that has a custom object that is a multi object type. I found an earlier discussion regarding this issue but it has been resolved. So I'm not sure if I'm doing anything wrong or if I found a situation where the issue still exist?
It looks like this when I read it out
"custom_fields": {
"ApplicationNodes": [
{
"id": 211,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/211/",
"display": "testnode1",
"name": "testnode1"
},
{
"id": 212,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/212/",
"display": "testnode2",
"name": "testnode2"
}
],
"ClusterManager": null,
"MasterClusterID": null
},
I have succeded in clear the ApplicationNodes custom object with this command:
curl -X 'PATCH'
'https://10.166.168.199/api/virtualization/clusters/19/'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-H 'X-CSRFTOKEN: yIYXUSHeuIjAbBDMQKkSCzxHQO9BaELwuNr5MGurtP4HqdPcKMRRT56MQLKw1Avm'
-d '{
"custom_fields": {
"ApplicationNodes":null
}
}'
Verify by read again:
"custom_fields": {
"ApplicationNodes": null,
"ClusterManager": null,
"MasterClusterID": null
},
Then I try to put the information back:
curl -X 'PATCH'
'https://10.166.168.199/api/virtualization/clusters/19/'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-H 'X-CSRFTOKEN: yIYXUSHeuIjAbBDMQKkSCzxHQO9BaELwuNr5MGurtP4HqdPcKMRRT56MQLKw1Avm'
-d '{
"custom_fields": {
"ApplicationNodes": [
{
"id": 211,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/211/",
"display": "testnode1",
"name": "testnode1"
},
{
"id": 212,
"url": "https://10.166.168.199/api/virtualization/virtual-machines/212/",
"display": "testnode2",
"name": "testnode2"
}
]
}
}'
I get code 400 Bad Request:
{
"custom_fields": [
"Unknown related object(s): [{'id': 211, 'url': 'https://10.166.168.199/api/virtualization/virtual-machines/211/', 'display': 'testnode1', 'name': 'testnode1'}, {'id': 212, 'url': 'https://10.166.168.199/api/virtualization/virtual-machines/212/', 'display': 'testnode2', 'name': 'testnode2'}]"
]
}
I'm running
Netbox version 3.5.3
Python version 3.8.16
Beta Was this translation helpful? Give feedback.
All reactions