http
HTTP客户端,允许在数据库内收发HTTP请求 (supabase)
仓库
pramsey/pgsql-http
https://github.com/pramsey/pgsql-http
源码
pgsql-http-1.7.0.tar.gz
pgsql-http-1.7.0.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_http | 1.7.0 | UTIL | MIT | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 4070 | http | 否 | 是 | 否 | 是 | 否 | 否 | - |
| 相关扩展 | pg_net pg_curl pgjwt pg_smtp_client gzip bzip zstd pgjq pgmb |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PGDG | 1.7.0 | 1817161514 | pg_http | - |
| RPM | PGDG | 1.7.0 | 1817161514 | pg_http_$v | - |
| DEB | PGDG | 1.7.0 | 1817161514 | postgresql-$v-http | - |
构建
您可以使用 pig build 命令构建 pg_http 扩展的 RPM 包:
pig build pkg pg_http # 构建 RPM 包
安装
您可以直接安装 pg_http 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:
pig repo add pgdg -u # 添加 PGDG 仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_http; # 当前活跃 PG 版本安装
pig ext install -y pg_http -v 18 # PG 18
pig ext install -y pg_http -v 17 # PG 17
pig ext install -y pg_http -v 16 # PG 16
pig ext install -y pg_http -v 15 # PG 15
pig ext install -y pg_http -v 14 # PG 14
dnf install -y pg_http_18 # PG 18
dnf install -y pg_http_17 # PG 17
dnf install -y pg_http_16 # PG 16
dnf install -y pg_http_15 # PG 15
dnf install -y pg_http_14 # PG 14
apt install -y postgresql-18-http # PG 18
apt install -y postgresql-17-http # PG 17
apt install -y postgresql-16-http # PG 16
apt install -y postgresql-15-http # PG 15
apt install -y postgresql-14-http # PG 14
创建扩展:
CREATE EXTENSION http;
用法
https://github.com/pramsey/pgsql-http
请求 / 响应结构定义:
Composite type "public.http_request"
Column | Type | Modifiers
--------------+-------------------+-----------
method | http_method |
uri | character varying |
headers | http_header[] |
content_type | character varying |
content | character varying |
Composite type "public.http_response"
Column | Type | Modifiers
--------------+-------------------+-----------
status | integer |
content_type | character varying |
headers | http_header[] |
content | character varying |
示例
使用 SQL 发送 HTTP GET 请求
CREATE EXTENSION http;
-- 获取响应内容
SELECT content FROM http_get('http://httpbun.com/');
-- 获取状态码和内容类型
SELECT status, content_type FROM http_get('http://httpbun.com/');
-- status | content_type
-- --------+--------------------------
-- 200 | text/html; charset=utf-8
-- 获取响应头
SELECT (unnest(headers)).* FROM http_get('http://httpbun.com/');
-- field | value
-- ---------------------------+--------------------------------------------------
-- Location | https://httpbun.com/
-- Date | Mon, 04 Nov 2024 09:00:36 GMT
-- Content-Length | 0
-- Connection | close
-- alt-svc | h3=":443"; ma=2592000
-- content-security-policy | frame-ancestors 'none'
-- content-type | text/html
-- date | Mon, 04 Nov 2024 09:00:37 GMT
-- strict-transport-security | max-age=31536000; includeSubDomains; preload
-- x-content-type-options | nosniff
-- x-powered-by | httpbun/af040d24038613575a85f74c2283ae79f8169927
-- (11 rows)
SELECT status, content::json->'form' AS form FROM http_post('http://httpbun.com/post', jsonb_build_object('myvar','myval','foo','bar'));
发送 HTTP PUT 请求:
SELECT status, content_type, content::json->>'data' AS data
FROM http_put('http://httpbun.com/put', 'some text', 'text/plain');
-- status | content_type | data
-- --------+------------------+-----------
-- 200 | application/json | some text
发送 HTTP POST 请求: