<commit_before>.. _aiohttp-client:

Client
======

.. module:: aiohttp

.. currentmodule:: aiohttp


Make a Request
--------------

Begin by importing the aiohttp module::

    import aiohttp

Now, let's try to get a web-page. For example let's get GitHub's public
time-line::

    async with aiohttp.ClientSession() as session:
        async with session.get('https://api.github.com/events') as resp:
            print(resp.status)
            print(await resp.text())

Now, we have a :class:`ClientSession` called ``session`` and
a :class:`ClientResponse` object called ``resp``. We can get all the
information we need from the response.  The mandatory parameter of
:meth:`ClientSession.get` coroutine is an HTTP url.

In order to make an HTTP POST request use :meth:`ClientSession.post` coroutine::

    session.post('http://httpbin.org/post', data=b'data')

Other HTTP methods are available as well::

    session.put('http://httpbin.org/put', data=b'data')
    session.delete('http://httpbin.org/delete')
    session.head('http://httpbin.org/get')
    session.options('http://httpbin.org/get')
    session.patch('http://httpbin.org/patch', data=b'data')

.. note::

   Don't create a session per request. Most likely you need a session
   per application which performs all requests altogether.

   A session contains a connection pool inside, connection reusage and
   keep-alives (both are on by default) may speed up total performance.


JSON Request
------------

Any of session's request methods like `request`, `get`, `post` etc accept
`json` parameter::

  async with aiohttp.ClientSession() as session:
      async with session.post(json={'test': 'object})


By default session uses python's standard `json` module for serialization.
But it is possible to use different `serializer`. `ClientSession` accepts `json_serialize`
parameter::

  import ujson

  async with aiohttp.ClientSession(json_serialize=ujson.dumps) as session:
      async with session.post(json={'test': 'object})

<commit_msg>Fix missing ' in docs (#2152)<commit_after>.. _aiohttp-client:

Client
======

.. module:: aiohttp

.. currentmodule:: aiohttp


Make a Request
--------------

Begin by importing the aiohttp module::

    import aiohttp

Now, let's try to get a web-page. For example let's get GitHub's public
time-line::

    async with aiohttp.ClientSession() as session:
        async with session.get('https://api.github.com/events') as resp:
            print(resp.status)
            print(await resp.text())

Now, we have a :class:`ClientSession` called ``session`` and
a :class:`ClientResponse` object called ``resp``. We can get all the
information we need from the response.  The mandatory parameter of
:meth:`ClientSession.get` coroutine is an HTTP url.

In order to make an HTTP POST request use :meth:`ClientSession.post` coroutine::

    session.post('http://httpbin.org/post', data=b'data')

Other HTTP methods are available as well::

    session.put('http://httpbin.org/put', data=b'data')
    session.delete('http://httpbin.org/delete')
    session.head('http://httpbin.org/get')
    session.options('http://httpbin.org/get')
    session.patch('http://httpbin.org/patch', data=b'data')

.. note::

   Don't create a session per request. Most likely you need a session
   per application which performs all requests altogether.

   A session contains a connection pool inside, connection reusage and
   keep-alives (both are on by default) may speed up total performance.


JSON Request
------------

Any of session's request methods like `request`, `get`, `post` etc accept
`json` parameter::

  async with aiohttp.ClientSession() as session:
      async with session.post(json={'test': 'object'})


By default session uses python's standard `json` module for serialization.
But it is possible to use different `serializer`. `ClientSession` accepts `json_serialize`
parameter::

  import ujson

  async with aiohttp.ClientSession(json_serialize=ujson.dumps) as session:
      async with session.post(json={'test': 'object'})


{'fraction_non_alphanumeric': 0.12834489593657086, 'fraction_numerical': 0.0009910802775024777, 'mean_word_length': 3.7271662763466042, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7100275', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1138, 'n_words': 412}