Creating route-target/VRF with API not retaining tenant id

80 views
Skip to first unread message

Callum

unread,
Nov 19, 2021, 6:03:49 AM11/19/21
to NetBox
Hey,

Using the below code with python requests:

data = { 'name': rt, 'description': vrf, 'tenant': tenant_id }
response = requests.post(url, data=data, headers=headers)

My "tenant_id" is an integer, I have also tried hard coding a number to see if it was something with my string breaking it:

data = { 'name': rt, 'description': vrf, 'tenant': 42 }
response = requests.post(url, data=data, headers=headers)

Both create the route-target however with no tenant

Is anyone able to share any working code?

Thanks!

Chris Russell

unread,
Nov 19, 2021, 7:43:31 AM11/19/21
to Callum, NetBox
Hi Callum,

 Out of interest, most use the pynetbox module for this ... you may wish to consider the module.

 Have you tried using the test functions in the <netbox.ip>/api/docs ?

 If I run POST using api docs using the below data (where tenant 1 is a valid tenant) - it holds the tenant just fine:

{
  "name": "test",
  "tenant": 1,
  "description": "string",
  "custom_fields": {}
}

  returning:

{
  "id": 1,
  "url": "http://192.168.1.135/api/ipam/route-targets/1/",
  "display": "test",
  "name": "test",
  "tenant": {
    "id": 1,
    "url": "http://192.168.1.135/api/tenancy/tenants/1/",
    "display": "wibble",
    "name": "wibble",
    "slug": "wibble"
  },
  "description": "string",
  "tags": [],
  "custom_fields": {},
  "created": "2021-11-19",
  "last_updated": "2021-11-19T12:39:14.087321Z"
}

 netbox = latest release

Ta

Chris

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/c468da31-8feb-467d-b10c-5d4fe77ef918n%40googlegroups.com.

Callum

unread,
Nov 19, 2021, 11:03:01 AM11/19/21
to NetBox
Hi Chris,

Weird, I tried the api docs as you suggested and it worked with no issues, I then copied exactly the same data into requests and it didn't work:

import requests
import json
        "name": "string",
        "tenant": 42,
        "description": "string"
}
headers = {'Authorization': "Token xxxxx"}
response = requests.post(url, data=data, headers=headers)
print(response)

I've tried pynetbox and that seems to work ok, jus find it very odd this specifically doesn't work

Thanks!

Chris Russell

unread,
Nov 19, 2021, 1:20:52 PM11/19/21
to Callum, NetBox
Hi Callum,

 I tested this on mine, same result - after some playing - the issue is you need to set json= the data and not data, hence your post becomes:

response = requests.post(url, json=data, headers=headers)

 Try this and let me know if it resolves

Ta

Chris

Callum

unread,
Dec 3, 2021, 8:45:16 AM12/3/21
to NetBox
Hi Chris,

Sorry it took so long, just tested and this works perfectly

Thanks very much!

Reply all
Reply to author
Forward
0 new messages