reference: Add response.status GUC (#329)

Related to https://github.com/PostgREST/postgrest/pull/1541
This commit is contained in:
Steve Chavez
2020-06-05 13:01:52 -05:00
committed by GitHub
parent ff8fd648bb
commit a869734140
+37 -2
View File
@@ -1496,7 +1496,7 @@ Notice that the variable should be set to an *array* of single-key objects rathe
.. _pre_req_headers: .. _pre_req_headers:
Setting headers via pre-request Setting headers via pre-request
------------------------------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
By using a :ref:`pre-request` function, you can add headers to GET/POST/PATCH/PUT/DELETE responses. By using a :ref:`pre-request` function, you can add headers to GET/POST/PATCH/PUT/DELETE responses.
As an example, let's add some cache headers for all requests that come from an Internet Explorer(6 or 7) browser. As an example, let's add some cache headers for all requests that come from an Internet Explorer(6 or 7) browser.
@@ -1529,10 +1529,41 @@ Now when you make a GET request to a table or view, you'll get the cache headers
... ...
.. _guc_resp_status:
Errors and HTTP Status Codes Setting Response Status Code
---------------------------- ----------------------------
You can set the ``response.status`` GUC to override the default status code PostgREST provides. For instance, the following function would replace the default ``200`` status code.
.. code-block:: postgres
create or replace function teapot() returns json as $$
begin
perform set_config('response.status', '418', true);
return json_build_object('message', 'The requested entity body is short and stout.',
'hint', 'Tip it over and pour it out.');
end;
$$ language plpgsql;
.. code-block:: http
GET /rpc/teapot HTTP/1.1
.. code-block:: http
HTTP/1.1 418 I'm a teapot
{"message" : "The requested entity body is short and stout.",
"hint" : "Tip it over and pour it out."}
If the status code is standard, PostgREST will complete the status message(**I'm a teapot** in this example).
.. _raise_error:
Raise errors with HTTP Status Codes
-----------------------------------
Stored procedures can return non-200 HTTP status codes by raising SQL exceptions. For instance, here's a saucy function that always responds with an error: Stored procedures can return non-200 HTTP status codes by raising SQL exceptions. For instance, here's a saucy function that always responds with an error:
.. code-block:: postgresql .. code-block:: postgresql
@@ -1558,6 +1589,10 @@ Calling the function returns HTTP 400 with the body
"code":"P0001" "code":"P0001"
} }
.. note::
Keep in mind that ``RAISE EXCEPTION`` will abort the transaction and rollback all changes. If you don't want this, you can instead use the :ref:`response.status GUC <guc_resp_status>`.
One way to customize the HTTP status code is by raising particular exceptions according to the PostgREST :ref:`error to status code mapping <status_codes>`. For example, :code:`RAISE insufficient_privilege` will respond with HTTP 401/403 as appropriate. One way to customize the HTTP status code is by raising particular exceptions according to the PostgREST :ref:`error to status code mapping <status_codes>`. For example, :code:`RAISE insufficient_privilege` will respond with HTTP 401/403 as appropriate.
For even greater control of the HTTP status code, raise an exception of the ``PTxyz`` type. For instance to respond with HTTP 402, raise 'PT402': For even greater control of the HTTP status code, raise an exception of the ``PTxyz`` type. For instance to respond with HTTP 402, raise 'PT402':