You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 24, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: docs/how-tos/providing-images-for-img.rst
+36-10Lines changed: 36 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -26,18 +26,42 @@ First, we need a public table for storing the files.
26
26
, blob bytea
27
27
);
28
28
29
-
Let's assume this table contains an image of two cute kittens with id 42.
30
-
We can retrieve this image in binary format from our PostgREST API by requesting :code:`/files?select=blob&id=eq.42` with the :code:`Accept: application/octet-stream` header.
31
-
Unfortunately, putting the URL into the :code:`src` of an :code:`<img>` tag will not work.
32
-
That's because browsers do not send the required :code:`Accept: application/octet-stream` header.
29
+
Let's assume this table contains an image of two cute kittens with id 42. We can retrieve this image in binary format from our PostgREST API by using :ref:`custom_media`:
30
+
31
+
.. code-block:: postgres
32
+
33
+
create domain "application/octet-stream" as bytea;
34
+
35
+
create or replace function file(id int) returns "application/octet-stream" as $$
36
+
select blob from files where id = file.id;
37
+
$$ language sql;
38
+
39
+
Now we can request the RPC endpoint :code:`/rpc/file?id=42` with the :code:`Accept: application/octet-stream` header.
Unfortunately, putting the URL into the :code:`src` of an :code:`<img>` tag will not work. That's because browsers do not send the required :code:`Accept: application/octet-stream` header.
48
+
Instead, the :code:`Accept: image/webp` header is sent by many web browsers by default.
49
+
50
+
Luckily we can change the accepted media type in the function like so:
51
+
52
+
.. code-block:: postgres
53
+
54
+
create domain "image/webp" as bytea;
55
+
56
+
create or replace function file(id int) returns "image/webp" as $$
57
+
select blob from files where id = file.id;
58
+
$$ language sql;
33
59
34
-
Luckily we can specify the accepted media types in the :ref:`raw-media-types` configuration variable.
35
-
In this case, the :code:`Accept: image/webp` header is sent by many web browsers by default, so let's add it to the configuration variable, like this: :code:`raw-media-types="image/webp"`.
36
60
Now, the image will be displayed in the HTML page:
Use more accurate headers according to the type of the files by using the :ref:`raw-media-types` configuration. For example, adding the ``raw-media-types="image/png"`` setting to the configuration file will allow you to use the ``Accept: image/png`` header:
515
+
create or replace get_image(id int) returns "image/png" as $$
0 commit comments