diff --git a/aiohttp/client.py b/aiohttp/client.py index 84b06defb6e..1a912ecfc0e 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -3,6 +3,7 @@ import asyncio import base64 import hashlib +import json import os import sys import traceback @@ -12,7 +13,7 @@ from yarl import URL from . import connector as connector_mod -from . import client_exceptions, client_reqrep, hdrs, http +from . import client_exceptions, client_reqrep, hdrs, http, payload from .client_exceptions import * # noqa from .client_exceptions import (ClientError, ClientOSError, ClientResponseError, ServerTimeoutError, @@ -45,8 +46,8 @@ class ClientSession: def __init__(self, *, connector=None, loop=None, cookies=None, headers=None, skip_auto_headers=None, - auth=None, request_class=ClientRequest, - response_class=ClientResponse, + auth=None, json_serialize=json.dumps, + request_class=ClientRequest, response_class=ClientResponse, ws_response_class=ClientWebSocketResponse, version=http.HttpVersion11, cookie_jar=None, connector_owner=True, raise_for_status=False, @@ -93,6 +94,7 @@ def __init__(self, *, connector=None, loop=None, cookies=None, self._connector_owner = connector_owner self._default_auth = auth self._version = version + self._json_serialize = json_serialize self._read_timeout = read_timeout self._conn_timeout = conn_timeout self._raise_for_status = raise_for_status @@ -164,6 +166,8 @@ def _request(self, method, url, *, if data is not None and json is not None: raise ValueError( 'data and json parameters can not be used at the same time') + elif json is not None: + data = payload.JsonPayload(json) if not isinstance(chunked, bool) and chunked is not None: warnings.warn( @@ -208,7 +212,7 @@ def _request(self, method, url, *, req = self._request_class( method, url, params=params, headers=headers, - skip_auto_headers=skip_headers, data=data, json=json, + skip_auto_headers=skip_headers, data=data, cookies=cookies, auth=auth, version=version, compress=compress, chunked=chunked, expect100=expect100, loop=self._loop, diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index a67d49a8a56..423347fa531 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -54,7 +54,7 @@ class ClientRequest: def __init__(self, method, url, *, params=None, headers=None, skip_auto_headers=frozenset(), - data=None, json=None, cookies=None, + data=None, cookies=None, auth=None, version=http.HttpVersion11, compress=None, chunked=None, expect100=False, loop=None, response_class=None, @@ -84,9 +84,6 @@ def __init__(self, method, url, *, if loop.get_debug(): self._source_traceback = traceback.extract_stack(sys._getframe(1)) - if json is not None: - data = payload.JsonPayload(json) - self.update_version(version) self.update_host(url) self.update_headers(headers) diff --git a/aiohttp/payload.py b/aiohttp/payload.py index 334dfb06905..ce40dd43668 100644 --- a/aiohttp/payload.py +++ b/aiohttp/payload.py @@ -289,10 +289,10 @@ class JsonPayload(BytesPayload): def __init__(self, value, encoding='utf-8', content_type='application/json', - *args, **kwargs): + dumps=json.dumps, *args, **kwargs): super().__init__( - json.dumps(value).encode(encoding), + dumps(value).encode(encoding), content_type=content_type, encoding=encoding, *args, **kwargs) diff --git a/docs/client_reference.rst b/docs/client_reference.rst index b0cd3b6e2fa..0ddb0569aff 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -43,7 +43,8 @@ The client session supports the context manager protocol for self closing. .. class:: ClientSession(*, connector=None, loop=None, cookies=None, \ headers=None, skip_auto_headers=None, \ - auth=None, version=aiohttp.HttpVersion11, \ + auth=None, json_serialize=json.dumps, \ + version=aiohttp.HttpVersion11, \ cookie_jar=None, read_timeout=None, conn_timeout=None, \ raise_for_status=False) @@ -100,6 +101,8 @@ The client session supports the context manager protocol for self closing. .. versionadded:: 0.22 + :param callable json_serialize: Json serializer function. (:func:`json.dumps` by default) + :param bool raise_for_status: Automatically call `raise_for_status()` for each response. (default is False) diff --git a/docs/web_reference.rst b/docs/web_reference.rst index 1a2d8866ecc..f22ae794142 100644 --- a/docs/web_reference.rst +++ b/docs/web_reference.rst @@ -241,7 +241,7 @@ and :ref:`aiohttp-web-signals` handlers. Clone itself with replacement some attributes. - Creates and returns a new instance of Request object. If no parameters + Creates and returns a new instance of Request object. If no parameters are given, an exact copy is returned. If a parameter is not passed, it will reuse the one from the current request object.