| 1 |
Receiving data |
| 2 |
-------------- |
| 3 |
|
| 4 |
So far, we've only returned data to the client, however, this is only half of the story. We also need to be able to |
| 5 |
control the data that we receive from the client. |
| 6 |
|
| 7 |
Here's the code we'll use to start: |
| 8 |
|
| 9 |
.. literalinclude:: /examples/data_transfer_objects/factory/tutorial/simple_receiving_data.py |
| 10 |
:language: python |
| 11 |
:linenos: |
| 12 |
|
| 13 |
To simplify our demonstration, we've reduced our data model back down to a single ``Person`` class, with ``name`` |
| 14 |
``age`` and ``email`` attributes. |
| 15 |
|
| 16 |
As before, ``ReadDTO`` is configured for the handler, and excludes the ``email`` attribute from return payloads. |
| 17 |
|
| 18 |
Our handler is now a :class:`@post() <litestar.handlers.post>` handler, that is annotated to both accept and return an |
| 19 |
instance of ``Person``. |
| 20 |
|
| 21 |
Litestar can natively decode request payloads into Python :func:`dataclasses <dataclasses.dataclass>`, so we don't |
| 22 |
*need* a DTO defined for the inbound data for this script to work. |
| 23 |
|
| 24 |
Now that we need to send data to the server to test our program, you can use a tool like |
| 25 |
`Postman <https://www.postman.com/>`_ or `Posting <https://github.com/darrenburns/posting?tab=readme-ov-file#posting>`_. Here's an example of a request/response payload: |
| 26 |
|
| 27 |
.. image:: images/simple_receive_data.png |
| 28 |
:align: center |