在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:pramsey/pgsql-http开源软件地址:https://github.com/pramsey/pgsql-http开源编程语言:C 83.4%开源软件介绍:PostgreSQL HTTP ClientMotivationWouldn't it be nice to be able to write a trigger that called a web service? Either to get back a result, or to poke that service into refreshing itself against the new state of the database? This extension is for that. ExamplesURL encode a string. SELECT urlencode('my special string''s & things?');
URL encode a JSON associative array. SELECT urlencode(jsonb_build_object('name','Colin & James','rate','50%'));
Run a GET request and see the content. SELECT content FROM http_get('http://httpbin.org/ip');
Run a GET request with an Authorization header. SELECT content::json->'headers'->>'Authorization' FROM http((
'GET',
'http://httpbin.org/headers',
ARRAY[http_header('Authorization','Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9')],
NULL,
NULL
)::http_request)
Read the SELECT status, content_type FROM http_get('http://httpbin.org/');
Show all the SELECT (unnest(headers)).* FROM http_get('http://httpbin.org/');
Use the PUT command to send a simple text document to a server. SELECT status, content_type, content::json->>'data' AS data
FROM http_put('http://httpbin.org/put', 'some text', 'text/plain');
Use the PATCH command to send a simple JSON document to a server. SELECT status, content_type, content::json->>'data' AS data
FROM http_patch('http://httpbin.org/patch', '{"this":"that"}', 'application/json');
Use the DELETE command to request resource deletion. SELECT status, content_type, content::json->>'url' AS url
FROM http_delete('http://httpbin.org/delete');
As a shortcut to send data to a GET request, pass a JSONB data argument. SELECT status, content::json->'args' AS args
FROM http_get('http://httpbin.org/get',
jsonb_build_object('myvar','myval','foo','bar')); To POST to a URL using a data payload instead of parameters embedded in the URL, encode the data in a JSONB as a data payload. SELECT status, content::json->'form' AS form
FROM http_post('http://httpbin.org/post',
jsonb_build_object('myvar','myval','foo','bar')); To access binary content, you must coerce the content from the default WITH
http AS (
SELECT * FROM http_get('http://httpbin.org/image/png')
),
headers AS (
SELECT (unnest(headers)).* FROM http
)
SELECT
http.content_type,
length(textsend(http.content)) AS length_binary,
headers.value AS length_headers
FROM http, headers
WHERE field = 'Content-Length';
To access only the headers you can do a HEAD-Request. This will not follow redirections. SELECT
http.status,
headers.value AS location
FROM
http_head('http://google.com') AS http
LEFT OUTER JOIN LATERAL (SELECT value
FROM unnest(http.headers)
WHERE field = 'Location') AS headers
ON true;
ConceptsEvery HTTP call is a made up of an
The utility functions, The
As seen in the examples, you can unspool the array of Functions
CURL OptionsSelect CURL options are available to set using the
For example, -- Set the PROXYPORT option
SELECT http_set_curlopt('CURLOPT_PROXYPORT', '12345');
-- List all currently set options
SELECT * FROM http_list_curlopt(); Will set the proxy port option for the lifetime of the database connection. You can reset all CURL options to their defaults using the Using this extension as a background automated process without supervision (e.g as a trigger) may have unintended consequences for other servers. It is considered a best practice to share contact information with your requests, so that administrators can reach you in case your HTTP calls get out of control. Certain API policies (e.g. Wikimedia User-Agent policy) may even require sharing specific contact information
with each request. Others may disallow (via For such cases you can set the SELECT http_set_curlopt('CURLOPT_USERAGENT',
'Examplebot/2.1 (+http://www.example.com/bot.html) Contact [email protected]');
SELECT status, content::json ->> 'user-agent' FROM http_get('http://httpbin.org/user-agent');
Keep-Alive & TimeoutsThe By default each request uses a fresh connection and assures that the connection is closed when the request is done. This behavior reduces the chance of consuming system resources (sockets) as the extension runs over extended periods of time. High-performance applications may wish to enable keep-alive and connection persistence to reduce latency and enhance throughput. The following GUC variable changes the behavior of the http extension to maintain connections as long as possible:
By default a 5 second timeout is set for the completion of a request. If a different timeout is desired the following GUC variable can be used to set it in milliseconds:
InstallationUNIXIf you have PostgreSQL (>= 9.3) devel packages and CURL devel packages installed (>= 0.7.20), you should have If you already installed a previous version and you just want to upgrade, then WindowsThere is a build available at postgresonline, not maintained by me. Why This is a Bad Idea
To Do
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论