You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/advanced.md
+65-1Lines changed: 65 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,4 +66,68 @@ nb = pynetbox.api(
66
66
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
67
67
)
68
68
nb.http_session = session
69
-
```
69
+
```
70
+
71
+
# File Uploads (Image Attachments)
72
+
73
+
Pynetbox supports file uploads for endpoints that accept them, such as image attachments. When you pass a file-like object (anything with a `.read()` method) to `create()`, pynetbox automatically detects it and uses multipart/form-data encoding instead of JSON.
74
+
75
+
## Creating an Image Attachment
76
+
77
+
```python
78
+
import pynetbox
79
+
80
+
nb = pynetbox.api(
81
+
'http://localhost:8000',
82
+
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
83
+
)
84
+
85
+
# Attach an image to a device
86
+
withopen('/path/to/image.png', 'rb') as f:
87
+
attachment = nb.extras.image_attachments.create(
88
+
object_type='dcim.device',
89
+
object_id=1,
90
+
image=f,
91
+
name='rack-photo.png'
92
+
)
93
+
```
94
+
95
+
## Using io.BytesIO
96
+
97
+
You can also use in-memory file objects:
98
+
99
+
```python
100
+
import io
101
+
import pynetbox
102
+
103
+
nb = pynetbox.api(
104
+
'http://localhost:8000',
105
+
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
106
+
)
107
+
108
+
# Create image from bytes
109
+
image_data =b'...'# Your image bytes
110
+
file_obj = io.BytesIO(image_data)
111
+
file_obj.name ='generated-image.png'# Optional: set filename
112
+
113
+
attachment = nb.extras.image_attachments.create(
114
+
object_type='dcim.device',
115
+
object_id=1,
116
+
image=file_obj
117
+
)
118
+
```
119
+
120
+
## Custom Filename and Content-Type
121
+
122
+
For more control, pass a tuple instead of a file object:
123
+
124
+
```python
125
+
withopen('/path/to/image.png', 'rb') as f:
126
+
attachment = nb.extras.image_attachments.create(
127
+
object_type='dcim.device',
128
+
object_id=1,
129
+
image=('custom-name.png', f, 'image/png')
130
+
)
131
+
```
132
+
133
+
The tuple format is `(filename, file_object)` or `(filename, file_object, content_type)`.
0 commit comments