Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions endpoint/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,48 @@ the **To sync** filter on the list view. Run the **Sync registry**
action to commit the changes to the live routing table — until then, the
new configuration is **not** served.

Usage
=====

Accessing request parameters
----------------------------

Code snippets receive the current HTTP request as the ``request``
variable. There are two ways to read the parameters sent by the client:

- ``request.params``: a flat ``dict`` populated by Odoo's dispatcher.
For ``http`` routes it merges the query string, form-encoded body
fields, uploaded files and path arguments; for ``json`` routes it
contains the parsed JSON body and path arguments. Convenient for
simple cases:

.. code:: python

name = request.params.get("your_name", "")

Since it is a plain ``dict``, repeated keys (e.g.
``?tag=a&tag=b``) collapse into a single value.

- ``request.httprequest.args``: a Werkzeug ``MultiDict`` containing
**only** the query string parameters, regardless of the route type.
Use it when you need to tell query string values apart from body
fields, or when the same key is sent multiple times:

.. code:: python

# GET /my/endpoint?tag=red&tag=blue&page=2
tags = request.httprequest.args.getlist("tag") # ["red", "blue"]
page = request.httprequest.args.get("page") # "2"

``getlist()`` is the only way to retrieve all the values of a
repeated query string key.

For form-encoded ``POST``/``PUT`` bodies,
``request.httprequest.form`` provides the same ``MultiDict`` interface
for body fields. A JSON body sent to an ``http`` route is not exposed
through ``request.params``; read it with ``request.get_json_data()``
instead.

Known issues / Roadmap
======================

Expand Down
37 changes: 37 additions & 0 deletions endpoint/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Accessing request parameters

Code snippets receive the current HTTP request as the ``request``
variable. There are two ways to read the parameters sent by the client:

- ``request.params``: a flat ``dict`` populated by Odoo's dispatcher.
For ``http`` routes it merges the query string, form-encoded body
fields, uploaded files and path arguments; for ``json`` routes it
contains the parsed JSON body and path arguments. Convenient for
simple cases:

```python
name = request.params.get("your_name", "")
```

Since it is a plain ``dict``, repeated keys (e.g.
``?tag=a&tag=b``) collapse into a single value.

- ``request.httprequest.args``: a Werkzeug ``MultiDict`` containing
**only** the query string parameters, regardless of the route type.
Use it when you need to tell query string values apart from body
fields, or when the same key is sent multiple times:

```python
# GET /my/endpoint?tag=red&tag=blue&page=2
tags = request.httprequest.args.getlist("tag") # ["red", "blue"]
page = request.httprequest.args.get("page") # "2"
```

``getlist()`` is the only way to retrieve all the values of a
repeated query string key.

For form-encoded ``POST``/``PUT`` bodies,
``request.httprequest.form`` provides the same ``MultiDict`` interface
for body fields. A JSON body sent to an ``http`` route is not exposed
through ``request.params``; read it with ``request.get_json_data()``
instead.
Loading