Skip to content

[BUG] Content type ignored when uploading media #285

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", line39, inwrapper_functionreturnwrapper(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^File"/tmp/.venv/lib/python3.11/site-packages/pydantic/_internal/_validate_call.py", line136, 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", line1036, inupload_mediaresponse_data=self.api_client.call_api(
^^^^^^^^^^^^^^^^^^^^^^^^^File"/tmp/.venv/lib/python3.11/site-packages/bandwidth/api_client.py", line275, incall_apiresponse_data=self.rest_client.request(
^^^^^^^^^^^^^^^^^^^^^^^^^File"/tmp/.venv/lib/python3.11/site-packages/bandwidth/rest.py", line183, inrequestrequest_body=json.dumps(body)
^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.11/json/__init__.py", line231, indumpsreturn_default_encoder.encode(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.11/json/encoder.py", line200, inencodechunks=self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.11/json/encoder.py", line258, initerencodereturn_iterencode(o, 0)
^^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.11/json/encoder.py", line180, indefaultraiseTypeError(f'Object of type {o.__class__.__name__} 'TypeError: Objectof type bytesisnotJSONserializable

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