Skip to content

Response Headers

Set custom HTTP response headers for the endpoint.

Syntax

<Header-Name>: <value>

Response headers use standard HTTP header format with a colon separator. Header names are case-insensitive.

Examples

Set Content-Type

sql
create function get_html_page()
returns text
language sql
as $$select '<html><body><h1>Hello</h1></body></html>'$$;

comment on function get_html_page() is
'HTTP GET
Content-Type: text/html';

Response includes: Content-Type: text/html

Multiple Headers

sql
create function get_api_data()
returns json
language sql
as $$select '{"status": "ok"}'::json$$;

comment on function get_api_data() is
'HTTP GET
Content-Type: application/json
Cache-Control: no-store
X-Custom-Header: custom-value';

Response includes all three headers.

Multi-Value Headers

Headers with the same name are combined:

sql
create function set_cookies()
returns text
language sql
as $$select 'OK'$$;

comment on function set_cookies() is
'HTTP GET
Set-Cookie: session=abc123
Set-Cookie: theme=dark
Set-Cookie: lang=en';

Response includes: Set-Cookie: session=abc123, theme=dark, lang=en

Cache Control

sql
create function get_static_config()
returns json
language sql
as $$select config from app_config where id = 1$$;

comment on function get_static_config() is
'HTTP GET
Cache-Control: public, max-age=3600';

Clients can cache the response for 1 hour.

Combined with Other Annotations

sql
create function export_report()
returns text
language sql
as $$...$$;

comment on function export_report() is
'HTTP GET
authorize manager
Content-Type: text/csv
Content-Disposition: attachment; filename="report.csv"
Cache-Control: no-cache';

Dynamic Headers from Parameters

Header values can include parameter values using the {param_name} template syntax:

sql
create function export_report(_type text, _file text)
returns text
language sql
as $$...$$;

comment on function export_report(text, text) is
'HTTP GET
authorize manager
Content-Type: {_type}
Content-Disposition: attachment; filename={_file}
Cache-Control: no-cache';

Request: GET /api/export-report?_type=text/csv&_file=report.csv

Response headers:

Content-Type: text/csv
Content-Disposition: attachment; filename=report.csv

CORS Headers

sql
create function cors_endpoint()
returns json
language sql
as $$select '{}'::json$$;

comment on function cors_endpoint() is
'HTTP GET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: Content-Type';

Note: For comprehensive CORS support, use the CORS configuration instead.

Common Headers

HeaderPurpose
Content-TypeResponse media type
Cache-ControlCaching directives
Content-DispositionDownload filename
X-*Custom application headers
Set-CookieSet cookies
  • RAW - Return raw text output
  • CACHED - Server-side caching

Released under the MIT License.