Skip to content

Commit 0da56ad

Browse files
committed
Updated uploadFile
1 parent 4367f78 commit 0da56ad

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

whatsapp_api_client_python/tools/sending.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pathlib
33
from typing import Dict, List, Optional, TYPE_CHECKING, Union
44

5+
from requests import Session
6+
57
from ..response import Response
68

79
if TYPE_CHECKING:
@@ -163,14 +165,21 @@ def uploadFile(self, path: str) -> Response:
163165
file_name = pathlib.Path(path).name
164166
content_type = mimetypes.guess_type(file_name)[0]
165167

166-
files = {"file": (file_name, open(path, "rb"), content_type)}
167-
168-
return self.api.request(
169-
"POST", (
170-
"{{media}}/waInstance{{idInstance}}/"
171-
"uploadFile/{{apiTokenInstance}}"
172-
), files=files
173-
)
168+
try:
169+
with open(path, "rb") as file:
170+
with Session() as session:
171+
response = session.request(
172+
method="POST",
173+
url=(
174+
f"{self.api.media}/waInstance{self.api.idInstance}/"
175+
f"uploadFile/{self.api.apiTokenInstance}"
176+
),
177+
data=file.read(),
178+
headers={"Content-Type": content_type}
179+
)
180+
except Exception as error:
181+
return Response(None, f"Other error occurred: {error}.")
182+
return Response(response.status_code, response.text)
174183

175184
def sendLocation(
176185
self,

0 commit comments

Comments
 (0)