Checklist
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 bytesisnotJSONserializableDebugging 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.
Checklist
Description
I was using the the Media API to upload an image for sending MMS, this is how my code looks like, pretty straightforward:
This raised the following error:
Debugging it a bit further I noted that even when the
Content-TypeHTTP header is set here, it's later overridden using the_content_typeargument here. Since I'm not supposed to pass_content_type, that value isNoneand the HTTP header is overwritten withapplication/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_typeas the desired content type.Environment Information
Expected Behavior
Passing
content_typeis enough forupload_media.Suggested Fix
Check that
Content-TypeHTTP header isNonebefore using the hard override_content_type.