Requests functions in the HTTP Client DuckDB extension
Function category
Requests
6 functionsIssue HTTP requests from DuckDB SQL. http_get for reads, http_post for JSON-body POSTs, http_post_form for application/x-www-form-urlencoded POSTs, http_head for headers only, and (DuckDB 2.0) http_put and http_delete. Each returns a JSON object with status, reason, and body (plus the response headers on DuckDB 2.0).
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
url
|
Type
VARCHAR
|
Mode Positional | Description The URL of the resource to delete. |
Argument
headers
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Optional request headers as a MAP, e.g. MAP{'accept': 'application/json'}. Names and values containing line breaks are rejected. On DuckDB 2.0 the parameter can be left out (NULL means no headers).
|
Returns
Same shape as http_get.
Description
New in the DuckDB 2.0 build. Issue an HTTP DELETE request (without a body).
DuckDB 2.0 build: requests go through DuckDB's own HTTP stack — https:// and every method other than plain-HTTP GET use the httpfs extension (autoloaded), so http_timeout, http_retries, http_proxy and HTTP secrets apply. Requests are refused when enable_external_access is false. Redirects are followed up to http_client_max_redirects (default 10) and re-checked on every hop: https:// is never redirected to http://, and Authorization/Cookie headers are dropped when a redirect changes host. Bodies larger than http_client_max_response_size (default 100 MiB) raise an error.
SELECT (http_delete('https://httpbin.org/delete')->>'status')::INT AS status;
Related functions
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
url
|
Type
VARCHAR
|
Mode Positional |
Description
The URL to fetch: https://... or http://.... On DuckDB 2.0 other schemes, URLs without a host, and URLs with spaces or control characters are rejected; user:password@ in the URL is sent as basic auth.
|
Argument
headers
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Optional request headers as a MAP, e.g. MAP{'accept': 'application/json'}. Names and values containing line breaks are rejected. On DuckDB 2.0 the parameter can be left out (NULL means no headers).
|
Argument
params
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Optional query parameters. Each entry is percent-encoded and appended to the URL's query string (with & if the URL already has one).
|
Returns
A JSON object with status (the HTTP status code as a number), reason (the HTTP reason phrase), body (the response body as a string) and, on DuckDB 2.0, headers (the response headers as a JSON object with lower-case names). A body that is not valid UTF-8 is returned base64-encoded with "body_encoding": "base64" (DuckDB 2.0).
Description
Issue an HTTP GET against url and return the response as JSON. Synchronous — DuckDB blocks until the response arrives or the request fails.
Extract fields from the result with DuckDB's JSON operators: ->> for string fields, -> for nested JSON. To dive into a JSON response body, cast it: ((http_get(...)->>'body')::JSON)->>'$.field'.
For endpoints that need request headers (auth tokens, content negotiation) or query parameters, pass headers and params: both are MAP(VARCHAR, VARCHAR), and params is appended to the URL as a query string.
A request that gets no response (DNS failure, refused connection, timeout) returns status -1 with the error message in reason rather than raising an error. HTTP error statuses (404, 500, ...) are returned as-is.
DuckDB 2.0 build: requests go through DuckDB's own HTTP stack — https:// and every method other than plain-HTTP GET use the httpfs extension (autoloaded), so http_timeout, http_retries, http_proxy and HTTP secrets apply. Requests are refused when enable_external_access is false. Redirects are followed up to http_client_max_redirects (default 10) and re-checked on every hop: https:// is never redirected to http://, and Authorization/Cookie headers are dropped when a redirect changes host. Bodies larger than http_client_max_response_size (default 100 MiB) raise an error.
SELECT (http_get('https://httpbin.org/get')->>'status')::INT AS status;
SELECT ((http_get(
'https://httpbin.org/get',
headers => MAP{'accept': 'application/json'},
params => MAP{'q': 'duckdb'}
)->>'body')::JSON)->>'$.args.q' AS q;
WITH r AS (
SELECT http_get('https://httpbin.org/uuid') AS res
)
SELECT ((res->>'body')::JSON)->>'$.uuid' AS uuid
FROM r;
SELECT u.id,
u.email,
((http_get('https://api.example.com/score/' || u.id::VARCHAR)->>'body')::JSON)->>'$.score' AS score
FROM users u
WHERE u.last_seen > now() - INTERVAL 1 HOUR
LIMIT 100;
Related functions
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
url
|
Type
VARCHAR
|
Mode Positional |
Description
The URL to issue the HEAD request against.
|
Argument
headers
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Optional request headers as a MAP, e.g. MAP{'accept': 'application/json'}. Names and values containing line breaks are rejected. On DuckDB 2.0 the parameter can be left out (NULL means no headers).
|
Returns
A JSON object with status, reason and headers — the response headers as a JSON object with lower-case names (repeated headers are joined with , ). No body.
Description
Issue an HTTP HEAD request. Returns {status, reason, headers} and no body. Use it for liveness checks or to test whether a URL exists before fetching the full payload with http_get.
Changed in the DuckDB 2.0 build: headers is a JSON object (res->'headers'->>'content-type'). Earlier builds returned it as a JSON-encoded string; (res->>'headers')::JSON works on both.
DuckDB 2.0 build: requests go through DuckDB's own HTTP stack — https:// and every method other than plain-HTTP GET use the httpfs extension (autoloaded), so http_timeout, http_retries, http_proxy and HTTP secrets apply. Requests are refused when enable_external_access is false. Redirects are followed up to http_client_max_redirects (default 10) and re-checked on every hop: https:// is never redirected to http://, and Authorization/Cookie headers are dropped when a redirect changes host. Bodies larger than http_client_max_response_size (default 100 MiB) raise an error.
SELECT (http_head('https://httpbin.org/')->>'status')::INT AS status;
SELECT http_head('https://httpbin.org/get')->'headers'->>'content-type' AS content_type;
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
url
|
Type
VARCHAR
|
Mode Positional | Description The URL to POST to. |
Argument
headers
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Optional request headers as a MAP, e.g. MAP{'accept': 'application/json'}. Names and values containing line breaks are rejected. On DuckDB 2.0 the parameter can be left out (NULL means no headers).
|
Argument
body
|
Type
JSON
|
Mode Named |
Description
DuckDB 2.0. The request body as JSON, sent with Content-Type: application/json unless headers sets another Content-Type. A STRUCT or MAP is converted to a JSON object. Optional: no body sends an empty POST.
|
Argument
params
|
Type
JSON
|
Mode Named |
Description
The request body, under the name earlier builds used. Still accepted on DuckDB 2.0 as an alias for body (passing both is an error); it is the only name on the Haybarn 1.5 build.
|
Returns
Same shape as http_get.
Description
Issue an HTTP POST with a JSON request body. Use this for REST APIs that consume JSON. On DuckDB 2.0 the body parameter is named body; params => keeps working, so the examples below run on both builds.
For application/x-www-form-urlencoded bodies (older REST APIs, OAuth token endpoints, HTML form handlers), use http_post_form instead.
Keep credentials out of cached query plans by binding them through SET VARIABLE + getenv, or (DuckDB 2.0) store them in an HTTP secret — see Securing credentials.
DuckDB 2.0 build: requests go through DuckDB's own HTTP stack — https:// and every method other than plain-HTTP GET use the httpfs extension (autoloaded), so http_timeout, http_retries, http_proxy and HTTP secrets apply. Requests are refused when enable_external_access is false. Redirects are followed up to http_client_max_redirects (default 10) and re-checked on every hop: https:// is never redirected to http://, and Authorization/Cookie headers are dropped when a redirect changes host. Bodies larger than http_client_max_response_size (default 100 MiB) raise an error.
SELECT http_post(
'https://httpbin.org/post',
headers => MAP{'accept': 'application/json',
'content-type': 'application/json'},
params => MAP{'name': 'alice', 'plan': 'pro'}
);
SET VARIABLE api_token = getenv('API_TOKEN');
SELECT http_post(
'https://api.example.com/users',
headers => MAP{
'authorization': 'Bearer ' || :api_token,
'content-type': 'application/json'
},
params => MAP{'email': '[email protected]'}
);
SELECT (http_post('https://httpbin.org/post',
headers => MAP{},
params => MAP{'k': 'v'})->>'status')::INT AS status;
Related functions
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
url
|
Type
VARCHAR
|
Mode Positional | Description The URL to POST to. |
Argument
headers
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Optional request headers as a MAP, e.g. MAP{'accept': 'application/json'}. Names and values containing line breaks are rejected. On DuckDB 2.0 the parameter can be left out (NULL means no headers).
|
Argument
params
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Form fields as a MAP. Each entry is URL-encoded into the request body using application/x-www-form-urlencoded.
|
Returns
Same shape as http_get.
Description
Issue an HTTP POST with the params map encoded as application/x-www-form-urlencoded — the body shape used by HTML forms, many older REST APIs, and OAuth 2.0 token endpoints.
The extension sets the request's Content-Type header to application/x-www-form-urlencoded for you; you don't need to put it in headers manually.
For JSON request bodies, use http_post instead.
DuckDB 2.0 build: requests go through DuckDB's own HTTP stack — https:// and every method other than plain-HTTP GET use the httpfs extension (autoloaded), so http_timeout, http_retries, http_proxy and HTTP secrets apply. Requests are refused when enable_external_access is false. Redirects are followed up to http_client_max_redirects (default 10) and re-checked on every hop: https:// is never redirected to http://, and Authorization/Cookie headers are dropped when a redirect changes host. Bodies larger than http_client_max_response_size (default 100 MiB) raise an error.
SELECT http_post_form(
'https://httpbin.org/post',
headers => MAP{},
params => MAP{'limit': '10', 'cursor': 'abc'}
);
SET VARIABLE client_id = getenv('OAUTH_CLIENT_ID');
SET VARIABLE client_secret = getenv('OAUTH_CLIENT_SECRET');
SELECT ((http_post_form(
'https://oauth.example.com/token',
headers => MAP{},
params => MAP{
'grant_type': 'client_credentials',
'client_id': :client_id,
'client_secret': :client_secret
}
)->>'body')::JSON)->>'$.access_token' AS access_token;
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
url
|
Type
VARCHAR
|
Mode Positional | Description The URL to PUT to. |
Argument
headers
|
Type
MAP(VARCHAR, VARCHAR)
|
Mode Named |
Description
Optional request headers as a MAP, e.g. MAP{'accept': 'application/json'}. Names and values containing line breaks are rejected. On DuckDB 2.0 the parameter can be left out (NULL means no headers).
|
Argument
body
|
Type
JSON
|
Mode Named |
Description
The request body as JSON, sent with Content-Type: application/json unless headers sets another Content-Type.
|
Returns
Same shape as http_get.
Description
New in the DuckDB 2.0 build. Issue an HTTP PUT with a JSON request body — for REST APIs that create or replace a resource at a known URL.
DuckDB 2.0 build: requests go through DuckDB's own HTTP stack — https:// and every method other than plain-HTTP GET use the httpfs extension (autoloaded), so http_timeout, http_retries, http_proxy and HTTP secrets apply. Requests are refused when enable_external_access is false. Redirects are followed up to http_client_max_redirects (default 10) and re-checked on every hop: https:// is never redirected to http://, and Authorization/Cookie headers are dropped when a redirect changes host. Bodies larger than http_client_max_response_size (default 100 MiB) raise an error.
SELECT (http_put(
'https://httpbin.org/put',
body => {'name': 'duck', 'plan': 'pro'}
)->>'status')::INT AS status;