Request failed with code 400 Bad Request when trying to use the NetBox API to create a cable connection #148894
Replies: 5 comments 1 reply
-
|
Hi Johan, The error you’re encountering suggests that the NetBox API requires both terminations (termination_a and termination_b) to be properly defined when creating a cable. Based on the code you’ve shared, the issue may be with the naming of the parameters or the exact structure of the create() function. In NetBox v4.x, the parameters for creating a cable have slightly changed. The terminations need to be specified as a list of dictionaries. Here’s an updated version of your code that should work: Updated Code: Initialize the NetBox APInb = pynetbox.api('http://10.129.201.216/', token='all long code') Define the terminationstermination_a = { try: Best regards |
Beta Was this translation helpful? Give feedback.
-
|
Hi, |
Beta Was this translation helpful? Give feedback.
-
|
I have the same problem when creating a rear port. tA = { "object_type": "dcim.rearport", "object_id": 69} Result: |
Beta Was this translation helpful? Give feedback.
-
|
This is most likely related to API changes introduced in newer NetBox 4.x versions. Older examples using: termination_a_type
termination_a_id
termination_b_type
termination_b_iddo not work correctly anymore for cable creation. Also, using: terminations=[...]still throws the same validation error on some newer NetBox releases because the API now expects explicit A/B termination groups. Try using: new_cable = nb.dcim.cables.create(
a_terminations=[
{
"object_type": "dcim.interface",
"object_id": 1326
}
],
b_terminations=[
{
"object_type": "dcim.interface",
"object_id": 1350
}
],
status="connected"
)The important part is using: a_terminations
b_terminationsinstead of the older single I hit something similar recently when working with NetBox 4.x + newer pynetbox versions, and the older examples from previous documentation/posts were still showing up in search results. Since your interfaces already exist and connectivity works from DataGrip, the issue is probably not authentication or networking but simply the payload structure expected by the newer API version. |
Beta Was this translation helpful? Give feedback.
-
|
The original code uses: termination_a_type That structure worked in older NetBox versions but no longer works properly in newer NetBox 4.x releases. Another incorrect suggestion in the thread is: terminations=[termination_a, termination_b] Even though it looks valid, newer NetBox versions often still reject it with: Must define A and B terminations when creating a new cable. because the API now expects explicit separation between side A and side B terminations. The correct fix is to use: a_terminations with list objects. Correct working example: import pynetbox nb = pynetbox.api( new_cable = nb.dcim.cables.create( print(new_cable) Why this fixes it: NetBox 4.x changed the cable creation payload format |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Hi all,
I'm unable to create a cable between two interfaces using the NetBox API. Receiving the error: pynetbox.core.query.RequestError: The request failed with code 400 Bad Request: {'all': ['Must define A and B terminations when creating a new cable.']}.
Details:
Interface A:
Device: evs-edge-1
Ethernet: Ethernet 11
ID: 1326
Interface B:
Device: evs-spine-1
Ethernet: Ethernet 1
ID: 1350
The ID's have been verified and are accessible thru the web interface.
Code:
Python
import pynetbox
Initialize the NetBox API
nb = pynetbox.api('http://10.129.201.216/', token='all long code')
Define the IDs and types for the terminations
termination_a_type = "dcim.interface"
termination_a_id = 1326 # Ethernet 11 on evs-edge-1
termination_b_type = "dcim.interface"
termination_b_id = 1350 # Ethernet 1 on evs-spine-1
try:
# Create the new cable
new_cable = nb.dcim.cables.create(
termination_a_type=termination_a_type,
termination_a_id=termination_a_id,
termination_b_type=termination_b_type,
termination_b_id=termination_b_id
)
print(f"Cable created successfully: {new_cable}")
except pynetbox.RequestError as e:
# Handle the request error
print(f"The request failed with code {e.response.status_code}: {e.error}")
NetBox release | NetBox Community v4.1.2 (2024-09-26)
Python version 3.12.6
Django version 5.0.9
Any help is welcome.
Johan
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions