Skip to content

Commit 3df2179

Browse files
WilliamBergaminClaude
andauthored
fix(web): use the request body for has_json and read async upload data (#2037)
Co-authored-by: Claude <svc-devxp-claude@slack-corp.com>
1 parent b6c005a commit 3df2179

6 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎slack_sdk/web/async_base_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,5 @@ async def _upload_file(
252252
)
253253
return FileUploadV2Result(
254254
status=result.get("status_code"), # type: ignore[arg-type]
255-
body=result.get("body"), # type: ignore[arg-type]
255+
body=result.get("data"), # type: ignore[arg-type]
256256
)

‎slack_sdk/web/base_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def convert_params(values: dict) -> dict:
288288

289289
request_headers = self._build_urllib_request_headers(
290290
token=token or self.token, # type: ignore[arg-type]
291-
has_json=json is not None,
291+
has_json=json_body is not None,
292292
has_files=files is not None,
293293
additional_headers=additional_headers,
294294
)

‎slack_sdk/web/legacy_base_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def convert_params(values: dict) -> dict:
360360

361361
request_headers = self._build_urllib_request_headers(
362362
token=token or self.token,
363-
has_json=json is not None,
363+
has_json=json_body is not None,
364364
has_files=files is not None,
365365
additional_headers=additional_headers,
366366
)

‎tests/slack_sdk/web/test_web_client.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import socket
33
import unittest
44
import time
5+
from unittest.mock import patch
56

67
import slack_sdk.errors as err
78
from slack_sdk import WebClient
@@ -246,3 +247,17 @@ def test_base_url_appends_trailing_slash_issue_15141(self):
246247
def test_base_url_preserves_trailing_slash_issue_15141(self):
247248
client = WebClient(base_url="http://localhost:8888/")
248249
self.assertEqual(client.base_url, "http://localhost:8888/")
250+
251+
def test_bodyless_requests_send_form_urlencoded_content_type(self):
252+
response = {"status": 200, "headers": {}, "body": '{"ok": true}'}
253+
with patch.object(self.client, "_perform_urllib_http_request", return_value=response) as transport:
254+
self.client.api_test()
255+
headers = transport.call_args[1]["args"]["headers"]
256+
self.assertEqual("application/x-www-form-urlencoded", headers["Content-Type"])
257+
258+
def test_json_requests_send_json_content_type(self):
259+
response = {"status": 200, "headers": {}, "body": '{"ok": true}'}
260+
with patch.object(self.client, "_perform_urllib_http_request", return_value=response) as transport:
261+
self.client.api_call("chat.postMessage", json={"channel": "C111", "text": "hi"})
262+
headers = transport.call_args[1]["args"]["headers"]
263+
self.assertEqual("application/json;charset=utf-8", headers["Content-Type"])

‎tests/slack_sdk_async/web/test_async_web_client.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import unittest
3+
from unittest.mock import patch
34

45
import slack_sdk.errors as err
56
from slack_sdk.models.blocks import DividerBlock
@@ -167,3 +168,20 @@ async def test_user_auth_blocks(self):
167168
user_auth_blocks=[DividerBlock(), DividerBlock()],
168169
)
169170
self.assertIsNone(new_message.get("error"))
171+
172+
@async_test
173+
async def test_upload_file_reports_the_response_body(self):
174+
async def fake_request(**kwargs):
175+
return {"status_code": 500, "data": "upload failed", "headers": {}}
176+
177+
with patch("slack_sdk.web.async_base_client._request_with_session", new=fake_request):
178+
result = await self.client._upload_file(
179+
url="https://files.slack.com/upload/v1/abc",
180+
data=b"hello",
181+
logger=self.client._logger,
182+
timeout=30,
183+
proxy=None,
184+
ssl=None,
185+
)
186+
self.assertEqual(500, result.status)
187+
self.assertEqual("upload failed", result.body)

‎tests/web/test_web_client.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import socket
55
import unittest
6+
from unittest.mock import patch
67

78
import slack.errors as err
89
from slack import WebClient
@@ -312,3 +313,10 @@ def test_default_team_id(self):
312313
client = WebClient(base_url="http://localhost:8888", team_id="T_DEFAULT")
313314
resp = client.users_list(token="xoxb-users_list_pagination")
314315
self.assertIsNone(resp["error"])
316+
317+
def test_bodyless_requests_send_form_urlencoded_content_type(self):
318+
response = {"status": 200, "headers": {}, "body": '{"ok": true}'}
319+
with patch.object(self.client, "_perform_urllib_http_request", return_value=response) as transport:
320+
self.client.api_test()
321+
headers = transport.call_args[1]["args"]["headers"]
322+
self.assertEqual("application/x-www-form-urlencoded", headers["Content-Type"])

0 commit comments

Comments
 (0)