Skip to content

Extras Image Attachment fails due to incorrect content type #520

@egreenspan2

Description

@egreenspan2

Attempting to attach an image to dcim.device using this sample code:

  with open("file_file.jpg", "rb") as file_handle:

        image_data = file_handle.read()

    base_encoded = base64.b64encode(image_data).decode("utf8")

    image_data = dict(
        content_type="dcim.device",
        object_id=1234,
        name="Test image",
        image=base_encoded,
        image_height=1234,
        image_width=1234,
    )
    try:
        nb_api.extras.image_attachments.create(image_data)
    except pynetbox.RequestError:
        log.exception(f"Failed to attach image")

Catching RequestError

   File "blah/venv/lib/python3.10/site-packages/pynetbox/core/query.py", line 287, in _make_call
       raise RequestError(req)
  pynetbox.core.query.RequestError: The request failed with code 400 Bad Request: {'image': ['The submitted data was not a file. Check the encoding type on the form.']}`

I believe the issue is that the Content-Type should be set to "multipart/form-data" for this to work.

Any suggestions are appreciated, is there a way to change content-type here? Or am I missing something simple?

Metadata

Metadata

Assignees

No one assigned

    Labels

    app: pynetboxseverity: lowDoes not significantly disrupt application functionality, or a workaround is availablestatus: needs ownerThis issue is tentatively accepted pending a volunteer committed to its implementationtype: bugA confirmed report of unexpected behavior in the application

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions