Skip to content
Database

http: RESTful 客户端

http 扩展允许你在 Postgres 中调用 RESTful 接口。

🌐 The http extension allows you to call RESTful endpoints within Postgres.

快速演示 #

🌐 Quick demo

概览 #

🌐 Overview

本节涵盖基本概念:

🌐 This section covers basic concepts:

  • REST:代表表述性状态转移。这是一种从外部服务请求数据的方式。
  • RESTful API 是接受 HTTP “调用”的服务器。这些调用通常是:
    • GET − 只能读取资源。
    • POST − 创建一个新资源。
    • DELETE − 移除一个资源。
    • PUT − 更新现有资源或创建新资源。

你可以使用 http 扩展从 Postgres 发起这些网络请求。

🌐 You can use the http extension to make these network requests from Postgres.

用法 #

🌐 Usage

启用扩展 #

🌐 Enable the extension

  1. 在仪表板中转到数据库页面。
  2. 点击侧边栏的 扩展
  3. 搜索 http 并启用这个扩展。

可用功能 #

🌐 Available functions

虽然主要用途是 http('http_request'),但有 5 个用于特定功能的封装函数:

🌐 While the main usage is http('http_request'), there are 5 wrapper functions for specific functionality:

  • http_get()
  • http_post()
  • http_put()
  • http_delete()
  • http_head()

返回的值 #

🌐 Returned values

http 扩展成功调用网页 URL 会返回一个包含以下字段的记录:

🌐 A successful call to a web URL from the http extension returns a record with the following fields:

  • status:integer
  • content_type:可变字符
  • headers:http_header[]
  • content:字符可变。通常你会想用 content::jsonb 的格式将其转换为 jsonb

示例 #

🌐 Examples

基础 GET#

🌐 Basic GET example [#simple-get-example]

1
select
2
"status", "content"::jsonb
3
from
4
extensions.http_get('https://jsonplaceholder.typicode.com/todos/1');

基础 POST#

🌐 Basic POST example [#simple-post-example]

1
select
2
"status", "content"::jsonb
3
from
4
extensions.http_post(
5
'https://jsonplaceholder.typicode.com/posts',
6
'{ "title": "foo", "body": "bar", "userId": 1 }',
7
'application/json'
8
);

资源 #

🌐 Resources