From 2d50d044fc750cf0d0c903e7f2ae17e1a707a05c Mon Sep 17 00:00:00 2001 From: pengfeiye Date: Fri, 5 Jun 2026 11:29:08 -0400 Subject: [PATCH] Fix JSON Content-Type header for draft and other API requests --- CHANGELOG.md | 1 + nylas/handler/http_client.py | 2 +- tests/handler/test_http_client.py | 15 ++++++++------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5e0eed..09b4946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ nylas-python Changelog ====================== Unreleased ---------- +* Fix draft and other JSON API requests failing with "only JSON and multipart supported" by sending `Content-Type: application/json` instead of `application/json; charset=utf-8` v6.15.0 ---------- diff --git a/nylas/handler/http_client.py b/nylas/handler/http_client.py index 023e2d1..2ded902 100644 --- a/nylas/handler/http_client.py +++ b/nylas/handler/http_client.py @@ -209,6 +209,6 @@ def _build_headers( if data is not None and data.content_type is not None: headers["Content-type"] = data.content_type elif response_body is not None: - headers["Content-type"] = "application/json; charset=utf-8" + headers["Content-type"] = "application/json" return {**headers, **extra_headers, **override_headers} diff --git a/tests/handler/test_http_client.py b/tests/handler/test_http_client.py index a0ae7d6..a80d3bd 100644 --- a/tests/handler/test_http_client.py +++ b/tests/handler/test_http_client.py @@ -63,7 +63,7 @@ def test_build_headers_json_body(self, http_client, patched_version_and_sys): "X-Nylas-API-Wrapper": "python", "User-Agent": "Nylas Python SDK 2.0.0 - 1.2.3", "Authorization": "Bearer test-key", - "Content-type": "application/json; charset=utf-8", + "Content-type": "application/json", } def test_build_headers_form_body(self, http_client, patched_version_and_sys): @@ -200,7 +200,7 @@ def test_execute_download_request_override_timeout( "X-Nylas-API-Wrapper": "python", "User-Agent": "Nylas Python SDK 2.0.0 - 1.2.3", "Authorization": "Bearer test-key", - "Content-type": "application/json; charset=utf-8", + "Content-type": "application/json", }, timeout=60, stream=False, @@ -299,7 +299,7 @@ def test_execute(self, http_client, patched_version_and_sys, patched_request): "X-Nylas-API-Wrapper": "python", "User-Agent": "Nylas Python SDK 2.0.0 - 1.2.3", "Authorization": "Bearer test-key", - "Content-type": "application/json; charset=utf-8", + "Content-type": "application/json", "test": "header", }, data=b'{"foo": "bar"}', @@ -332,7 +332,7 @@ def test_execute_with_serialized_json_body( "X-Nylas-API-Wrapper": "python", "User-Agent": "Nylas Python SDK 2.0.0 - 1.2.3", "Authorization": "Bearer test-key", - "Content-type": "application/json; charset=utf-8", + "Content-type": "application/json", }, data=canonical, timeout=30, @@ -347,7 +347,7 @@ def test_build_request_sets_content_type_for_serialized_json_body( request_body=None, serialized_json_body=b"{}", ) - assert request["headers"]["Content-type"] == "application/json; charset=utf-8" + assert request["headers"]["Content-type"] == "application/json" def test_execute_override_timeout( self, http_client, patched_version_and_sys, patched_request @@ -376,7 +376,7 @@ def test_execute_override_timeout( "X-Nylas-API-Wrapper": "python", "User-Agent": "Nylas Python SDK 2.0.0 - 1.2.3", "Authorization": "Bearer test-key", - "Content-type": "application/json; charset=utf-8", + "Content-type": "application/json", "test": "header", }, data=b'{"foo": "bar"}', @@ -466,7 +466,7 @@ def test_execute_with_headers(self, http_client, patched_version_and_sys, patche "X-Nylas-API-Wrapper": "python", "User-Agent": "Nylas Python SDK 2.0.0 - 1.2.3", "Authorization": "Bearer test-key", - "Content-type": "application/json; charset=utf-8", + "Content-type": "application/json", "test": "header", }, data=b'{"foo": "bar"}', @@ -497,6 +497,7 @@ def test_execute_with_utf8_characters(self, http_client, patched_version_and_sys assert response_json == {"success": True} # Verify that the data is sent as UTF-8 encoded bytes call_kwargs = patched_request.call_args[1] + assert call_kwargs["headers"]["Content-type"] == "application/json" assert "data" in call_kwargs sent_data = call_kwargs["data"]