Skip to content

[BUG] Content type ignored when uploading media #285

@dnohales

Description

@dnohales

Checklist

  • Have you provided a description of the bug?
  • Have you provided your Environment information?
  • Have you provided a sample code snippet?
  • Have you provided a stack trace?
  • Have you outlined the expected behavior?

Description

I was using the the Media API to upload an image for sending MMS, this is how my code looks like, pretty straightforward:

media_api.upload_media(
    account_id=BANDWIDTH_ACCOUNT_ID,
    media_id=my_media_id,
    body=my_content_in_bytes,
    content_type="image/png",
)

This raised the following error:

    media_api.upload_media(
  File "/tmp/.venv/lib/python3.11/site-packages/pydantic/_internal/_validate_call.py", line 39, in wrapper_function
    return wrapper(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/lib/python3.11/site-packages/pydantic/_internal/_validate_call.py", line 136, in __call__
    res = self.__pydantic_validator__.validate_python(pydantic_core.ArgsKwargs(args, kwargs))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/lib/python3.11/site-packages/bandwidth/api/media_api.py", line 1036, in upload_media
    response_data = self.api_client.call_api(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/lib/python3.11/site-packages/bandwidth/api_client.py", line 275, in call_api
    response_data = self.rest_client.request(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/.venv/lib/python3.11/site-packages/bandwidth/rest.py", line 183, in request
    request_body = json.dumps(body)
                   ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

Debugging it a bit further I noted that even when the Content-Type HTTP header is set here, it's later overridden using the _content_type argument here. Since I'm not supposed to pass _content_type, that value is None and the HTTP header is overwritten with application/json.

Down the line, the REST client tries to parse my raw bytes as if they are a JSON object and it fails to serialize it.

A workaround is just pass _content_type as the desired content type.

Environment Information

  • OS Version: Linux (Docker)
  • SDK Version: 22.1.0
  • Environment: 3.11.13

Expected Behavior

Passing content_type is enough for upload_media.

Suggested Fix

Check that Content-Type HTTP header is None before using the hard override _content_type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions