CREATE EXTENSION http;
|
set http.timeout_msec = 10000;
|
SELECT http_set_curlopt('CURLOPT_TIMEOUT', '10');
|
http_set_curlopt
|
------------------
|
t
|
(1 row)
|
|
-- Status code
|
SELECT status
|
FROM http_get('http://localhost:9080/status/202');
|
status
|
--------
|
202
|
(1 row)
|
|
-- Headers
|
SELECT lower(field) AS field, value
|
FROM (
|
SELECT (unnest(headers)).*
|
FROM http_get('http://localhost:9080/response-headers?Abcde=abcde')
|
) a
|
WHERE field ILIKE 'Abcde';
|
field | value
|
-------+-------
|
abcde | abcde
|
(1 row)
|
|
-- GET
|
SELECT status,
|
content::json->'args'->>'foo' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_get('http://localhost:9080/anything?foo=bar');
|
status | args | url | method
|
--------+------+----------------------------------------+--------
|
200 | bar | http://localhost:9080/anything?foo=bar | GET
|
(1 row)
|
|
-- GET with data
|
SELECT status,
|
content::json->'args'->>'this' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_get('http://localhost:9080/anything', jsonb_build_object('this', 'that'));
|
status | args | url | method
|
--------+------+------------------------------------------+--------
|
200 | that | http://localhost:9080/anything?this=that | GET
|
(1 row)
|
|
-- GET with data
|
SELECT status,
|
content::json->>'args' as args,
|
(content::json)->>'data' as data,
|
content::json->>'url' as url,
|
content::json->>'method' as method
|
FROM http(('GET', 'http://localhost:9080/anything', NULL, 'application/json', '{"search": "toto"}', NULL, NULL));
|
status | args | data | url | method
|
--------+------+--------------------+--------------------------------+--------
|
200 | {} | {"search": "toto"} | http://localhost:9080/anything | GET
|
(1 row)
|
|
-- DELETE
|
SELECT status,
|
content::json->'args'->>'foo' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_delete('http://localhost:9080/anything?foo=bar');
|
status | args | url | method
|
--------+------+----------------------------------------+--------
|
200 | bar | http://localhost:9080/anything?foo=bar | DELETE
|
(1 row)
|
|
-- DELETE with payload
|
SELECT status,
|
content::json->'args'->>'foo' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method,
|
content::json->>'data' AS data
|
FROM http_delete('http://localhost:9080/anything?foo=bar', 'payload', 'text/plain');
|
status | args | url | method | data
|
--------+------+----------------------------------------+--------+---------
|
200 | bar | http://localhost:9080/anything?foo=bar | DELETE | payload
|
(1 row)
|
|
-- PUT
|
SELECT status,
|
content::json->>'data' AS data,
|
content::json->'args'->>'foo' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_put('http://localhost:9080/anything?foo=bar','payload','text/plain');
|
status | data | args | url | method
|
--------+---------+------+----------------------------------------+--------
|
200 | payload | bar | http://localhost:9080/anything?foo=bar | PUT
|
(1 row)
|
|
-- PATCH
|
SELECT status,
|
content::json->>'data' AS data,
|
content::json->'args'->>'foo' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_patch('http://localhost:9080/anything?foo=bar','{"this":"that"}','application/json');
|
status | data | args | url | method
|
--------+-----------------+------+----------------------------------------+--------
|
200 | {"this":"that"} | bar | http://localhost:9080/anything?foo=bar | PATCH
|
(1 row)
|
|
-- POST
|
SELECT status,
|
content::json->>'data' AS data,
|
content::json->'args'->>'foo' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_post('http://localhost:9080/anything?foo=bar','payload','text/plain');
|
status | data | args | url | method
|
--------+---------+------+----------------------------------------+--------
|
200 | payload | bar | http://localhost:9080/anything?foo=bar | POST
|
(1 row)
|
|
SET client_min_messages TO DEBUG1;
|
-- GET with custom timeout and retries
|
SELECT status,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_get('http://localhost:5050/anything?ping=test', 2000, 10);
|
LOG: Q.DataBase: Could not stat the license file.
|
DEBUG: pgsql-http: http_retry was set '10'
|
DEBUG: retries left 9
|
DEBUG: retries left 8
|
DEBUG: retries left 7
|
DEBUG: retries left 6
|
DEBUG: retries left 5
|
DEBUG: retries left 4
|
DEBUG: retries left 3
|
DEBUG: retries left 2
|
DEBUG: retries left 1
|
DEBUG: retries left 0
|
ERROR: Failed to connect to localhost port 5050 after 0 ms: Connection refused
|
-- POST with payload and custom timeout/retries
|
SELECT status,
|
content::json->>'data' AS data,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_post('http://localhost:5050/anything', 'payload_data', 'text/plain', 3000, 5);
|
LOG: Q.DataBase: Could not stat the license file.
|
DEBUG: pgsql-http: http_retry was set '5'
|
DEBUG: retries left 4
|
DEBUG: retries left 3
|
DEBUG: retries left 2
|
DEBUG: retries left 1
|
DEBUG: retries left 0
|
ERROR: Failed to connect to localhost port 5050 after 0 ms: Connection refused
|
-- PUT with payload and custom timeout/retries
|
SELECT status,
|
content::json->>'data' AS data,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_put('http://localhost:9080/anything', 'updated_data', 'text/plain', 1500, 2);
|
LOG: Q.DataBase: Could not stat the license file.
|
DEBUG: pgsql-http: http_retry was set '2'
|
status | data | url | method
|
--------+--------------+--------------------------------+--------
|
200 | updated_data | http://localhost:9080/anything | PUT
|
(1 row)
|
|
-- GET with NULL timeout and NULL retries → fallback to default
|
SELECT status,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_get('http://localhost:5050/anything?nulls=test', NULL, NULL);
|
LOG: Q.DataBase: Could not stat the license file.
|
DEBUG: retries left 0
|
ERROR: Failed to connect to localhost port 5050 after 0 ms: Connection refused
|
-- GET with negative timeout and retries → fallback to default
|
SELECT status,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_get('http://localhost:5050/anything?negative=test', -500, -3);
|
LOG: Q.DataBase: Could not stat the license file.
|
DEBUG: pgsql-http: http_retry was set '1'
|
DEBUG: retries left 0
|
ERROR: Failed to connect to localhost port 5050 after 0 ms: Connection refused
|
SET client_min_messages TO WARNING;
|
-- POST with json data
|
SELECT status,
|
content::json->'form'->>'this' AS args,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_post('http://localhost:9080/anything', jsonb_build_object('this', 'that'));
|
status | args | url | method
|
--------+------+--------------------------------+--------
|
200 | that | http://localhost:9080/anything | POST
|
(1 row)
|
|
-- POST with data
|
SELECT status,
|
content::json->'form'->>'key1' AS key1,
|
content::json->'form'->>'key2' AS key2,
|
content::json->>'url' AS url,
|
content::json->>'method' AS method
|
FROM http_post('http://localhost:9080/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded');
|
status | key1 | key2 | url | method
|
--------+--------+--------+--------------------------------+--------
|
200 | value1 | value2 | http://localhost:9080/anything | POST
|
(1 row)
|
|
-- HEAD
|
SELECT lower(field) AS field, value
|
FROM (
|
SELECT (unnest(headers)).*
|
FROM http_head('http://localhost:9080/response-headers?Abcde=abcde')
|
) a
|
WHERE field ILIKE 'Abcde';
|
field | value
|
-------+-------
|
abcde | abcde
|
(1 row)
|
|
-- Follow redirect
|
SELECT status,
|
(content::json)->>'url' AS url
|
FROM http_get('http://localhost:9080/redirect-to?url=get');
|
status | url
|
--------+---------------------------
|
200 | http://localhost:9080/get
|
(1 row)
|
|
-- Request image
|
WITH
|
http AS (
|
SELECT * FROM http_get('http://localhost:9080/image/png')
|
),
|
headers AS (
|
SELECT (unnest(headers)).* FROM http
|
)
|
SELECT
|
http.content_type,
|
length(text_to_bytea(http.content)) AS length_binary
|
FROM http, headers
|
WHERE field ilike 'Content-Type';
|
content_type | length_binary
|
--------------+---------------
|
image/png | 8090
|
(1 row)
|
|
-- Alter options and and reset them and throw errors
|
SELECT http_set_curlopt('CURLOPT_PROXY', '127.0.0.1');
|
http_set_curlopt
|
------------------
|
t
|
(1 row)
|
|
-- Error because proxy is not there
|
DO $$
|
BEGIN
|
SELECT status FROM http_get('http://localhost:9080/status/555');
|
EXCEPTION
|
WHEN OTHERS THEN
|
RAISE WARNING 'Failed to connect';
|
END;
|
$$;
|
WARNING: Failed to connect
|
-- Still an error
|
DO $$
|
BEGIN
|
SELECT status FROM http_get('http://localhost:9080/status/555');
|
EXCEPTION
|
WHEN OTHERS THEN
|
RAISE WARNING 'Failed to connect';
|
END;
|
$$;
|
WARNING: Failed to connect
|
-- Reset options
|
SELECT http_reset_curlopt();
|
http_reset_curlopt
|
--------------------
|
t
|
(1 row)
|
|
-- Now it should work
|
SELECT status FROM http_get('http://localhost:9080/status/555');
|
status
|
--------
|
555
|
(1 row)
|
|
-- Alter the default timeout and then run a query that is longer than
|
-- the default (5s), but shorter than the new timeout
|
SELECT http_set_curlopt('CURLOPT_TIMEOUT_MS', '10000');
|
http_set_curlopt
|
------------------
|
t
|
(1 row)
|
|
SELECT status FROM http_get('http://localhost:9080/delay/7');
|
status
|
--------
|
200
|
(1 row)
|
|
-- Check that statement interruption works
|
SET statement_timeout = 200;
|
CREATE TEMPORARY TABLE timer AS
|
SELECT now() AS start;
|
SELECT *
|
FROM http_get('http://localhost:9080/delay/7');
|
ERROR: canceling statement due to user request
|
SELECT round(extract(epoch FROM now() - start) * 10) AS m
|
FROM timer;
|
m
|
---
|
2
|
(1 row)
|
|
DROP TABLE timer;
|
DROP EXTENSION http;
|