Skip to content

Commit a320c09

Browse files
authored
fix: Correctly send JSON data to the server (#121)
1 parent c3282d2 commit a320c09

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

tests/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ def test_patch_object(httpserver):
189189
httpserver.expect_request(
190190
'/api/v3/dummy_types/dummy_id',
191191
method='PATCH',
192-
headers={'X-Apikey': 'dummy_api_key'},
193-
data=json.dumps({'data': obj.to_dict(modified_attributes_only=True)}),
192+
headers={'X-Apikey': 'dummy_api_key', 'Content-Type': 'application/json'},
193+
json={'data': obj.to_dict(modified_attributes_only=True)},
194194
).respond_with_json({
195195
'data': {
196196
'id': 'dummy_id',
@@ -213,8 +213,8 @@ def test_post_object(httpserver):
213213
httpserver.expect_request(
214214
'/api/v3/dummy_types',
215215
method='POST',
216-
headers={'X-Apikey': 'dummy_api_key'},
217-
data=json.dumps({'data': obj.to_dict()}),
216+
headers={'X-Apikey': 'dummy_api_key', 'Content-Type': 'application/json'},
217+
json={'data': obj.to_dict()},
218218
).respond_with_json({
219219
'data': {
220220
'id': 'dummy_id',

vt/client.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ async def get_object_async(self, path, *path_args, params=None):
532532
response = await self.get_async(path, *path_args, params=params)
533533
return await self._response_to_object(response)
534534

535-
def patch(self, path, *path_args, data=None):
535+
def patch(self, path, *path_args, data=None, json_data=None):
536536
"""Sends a PATCH request to a given API endpoint.
537537
538538
This is a low-level function that returns a raw HTTP response, no error
@@ -543,18 +543,20 @@ def patch(self, path, *path_args, data=None):
543543
:param path_args: A variable number of arguments that are put into any
544544
placeholders used in path.
545545
:param data: Data sent in the request body.
546+
:param json_data: dict containing data to send in the request body as JSON.
546547
:type path: str
547548
:type data: A string or bytes
549+
:type json_data: dict
548550
:returns: An instance of :class:`ClientResponse`.
549551
"""
550-
return make_sync(self.patch_async(path, *path_args, data))
552+
return make_sync(self.patch_async(path, *path_args, data, json_data))
551553

552-
async def patch_async(self, path, *path_args, data=None):
554+
async def patch_async(self, path, *path_args, data=None, json_data=None):
553555
"""Like :func:`patch` but returns a coroutine."""
554556
return ClientResponse(
555557
await self._get_session().patch(
556558
self._full_url(path, *path_args),
557-
data=data, proxy=self._proxy))
559+
data=data, json=json_data, proxy=self._proxy))
558560

559561
def patch_object(self, path, *path_args, obj):
560562
"""Sends a PATCH request for modifying an object.
@@ -575,16 +577,12 @@ def patch_object(self, path, *path_args, obj):
575577

576578
async def patch_object_async(self, path, *path_args, obj):
577579
"""Like :func:`patch_object` but returns a coroutine."""
578-
data = json.dumps({'data': obj.to_dict(modified_attributes_only=True)})
580+
data = {'data': obj.to_dict(modified_attributes_only=True)}
579581

580-
if self._user_headers is None:
581-
self._user_headers = {}
582-
self._user_headers['Content-Type'] = 'application/json'
583-
584-
response = await self.patch_async(path, *path_args, data=data)
582+
response = await self.patch_async(path, *path_args, json_data=data)
585583
return await self._response_to_object(response)
586584

587-
def post(self, path, *path_args, data=None):
585+
def post(self, path, *path_args, data=None, json_data=None):
588586
"""Sends a POST request to a given API endpoint.
589587
590588
This is a low-level function that returns a raw HTTP response, no error
@@ -595,18 +593,21 @@ def post(self, path, *path_args, data=None):
595593
:param path_args: A variable number of arguments that are put into any
596594
placeholders used in path.
597595
:param data: Data sent in the request body.
596+
:param json_data: dict containing data to send in the request body as JSON.
598597
:type path: str
599598
:type data: A string or bytes
599+
:type json_data: dict
600600
:returns: An instance of :class:`ClientResponse`.
601601
"""
602-
return make_sync(self.post_async(path, *path_args, data=data))
602+
return make_sync(
603+
self.post_async(path, *path_args, data=data, json_data=json_data))
603604

604-
async def post_async(self, path, *path_args, data=None):
605+
async def post_async(self, path, *path_args, data=None, json_data=None):
605606
"""Like :func:`post` but returns a coroutine."""
606607
return ClientResponse(
607608
await self._get_session().post(
608609
self._full_url(path, *path_args),
609-
data=data, proxy=self._proxy))
610+
data=data, json=json_data, proxy=self._proxy))
610611

611612
def post_object(self, path, *path_args, obj):
612613
"""Sends a POST request for creating an object.
@@ -627,13 +628,9 @@ def post_object(self, path, *path_args, obj):
627628

628629
async def post_object_async(self, path, *path_args, obj):
629630
"""Like :func:`post_object` but returns a coroutine."""
630-
data = json.dumps({'data': obj.to_dict()})
631-
632-
if self._user_headers is None:
633-
self._user_headers = {}
634-
self._user_headers['Content-Type'] = 'application/json'
631+
data = {'data': obj.to_dict()}
635632

636-
response = await self.post_async(path, *path_args, data=data)
633+
response = await self.post_async(path, *path_args, json_data=data)
637634
return await self._response_to_object(response)
638635

639636
def iterator(self, path, *path_args, params=None, cursor=None,

0 commit comments

Comments
 (0)