Skip to content

Requests functions in the HTTP Client DuckDB extension

Function category

Requests

6 functions

http_delete

Scalar function Requests
Signature
http_delete(url: VARCHAR, headers := MAP(VARCHAR, VARCHAR)) → JSON
Arguments
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
Description
1 Delete a resource and check the status
SELECT (http_delete('https://httpbin.org/delete')->>'status')::INT AS status;

http_get

Scalar function Requests
Signature
http_get( url: VARCHAR, headers := MAP(VARCHAR, VARCHAR), params := MAP(VARCHAR, VARCHAR) ) → JSON
Arguments
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
Description
1 Read just the status code
SELECT (http_get('https://httpbin.org/get')->>'status')::INT AS status;
2 Send request headers and query parameters
SELECT ((http_get(
  'https://httpbin.org/get',
  headers => MAP{'accept': 'application/json'},
  params  => MAP{'q': 'duckdb'}
)->>'body')::JSON)->>'$.args.q' AS q;
3 Pull a field out of a JSON response body
WITH r AS (
  SELECT http_get('https://httpbin.org/uuid') AS res
)
SELECT ((res->>'body')::JSON)->>'$.uuid' AS uuid
FROM r;
4 Per-row enrichment against a REST API
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;

http_head

Scalar function Requests
Signature
http_head(url: VARCHAR, headers := MAP(VARCHAR, VARCHAR)) → JSON
Arguments
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
Description
1 Liveness check — is the URL reachable?
SELECT (http_head('https://httpbin.org/')->>'status')::INT AS status;
2 Read one response header (DuckDB 2.0)
SELECT http_head('https://httpbin.org/get')->'headers'->>'content-type' AS content_type;

http_post

Scalar function Requests
Signature
http_post( url: VARCHAR, body := JSON, headers := MAP(VARCHAR, VARCHAR), params := JSON ) → JSON
Arguments
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
Description
1 POST a JSON body
SELECT http_post(
  'https://httpbin.org/post',
  headers => MAP{'accept': 'application/json',
                 'content-type': 'application/json'},
  params  => MAP{'name': 'alice', 'plan': 'pro'}
);
2 Authorize with a Bearer token sourced from the environment
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]'}
);
3 Read just the status to verify the call succeeded
SELECT (http_post('https://httpbin.org/post',
                  headers => MAP{},
                  params  => MAP{'k': 'v'})->>'status')::INT AS status;

http_post_form

Scalar function Requests
Signature
http_post_form( url: VARCHAR, headers := MAP(VARCHAR, VARCHAR), params := MAP(VARCHAR, VARCHAR) ) → JSON
Arguments
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
Description
1 Submit a form to httpbin
SELECT http_post_form(
  'https://httpbin.org/post',
  headers => MAP{},
  params  => MAP{'limit': '10', 'cursor': 'abc'}
);
2 Exchange OAuth client credentials for an access token
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;

http_put

Scalar function Requests
Signature
http_put( url: VARCHAR, body := JSON, headers := MAP(VARCHAR, VARCHAR) ) → JSON
Arguments
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
Description
1 Replace a resource with a JSON document
SELECT (http_put(
  'https://httpbin.org/put',
  body => {'name': 'duck', 'plan': 'pro'}
)->>'status')::INT AS status;