The documentation you are viewing is for Dapr v1.6 which is an older version of Dapr. For up-to-date documentation, see the latest version.

HTTP 绑定规范

HTTP 绑定组件的详细文档

设置 Dapr 组件

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: <NAME>
  namespace: <NAMESPACE>
spec:
  type: bindings.http
  version: v1
  metadata:
  - name: url
    value: http://something.com

元数据字段规范

字段 必填 绑定支持 详情 示例
url 输出 要调用的 HTTP 终点的 base URL http://host:port/path, http://myservice:8000/customers

绑定支持

此组件支持以下 HTTP methods/verbs 输出绑定

  • create : 为了向后的兼容,并被当作一个 post 请求处理
  • get : 读数据或者记录
  • head : 连接服务器但不返回响应正文
  • post : 通常用于创建记录或发送命令
  • put : 更新数据或者记录
  • patch : 有时用于更新记录的字段子集
  • delete : 删除数据或者记录
  • options : 请求提供关于可用通信选项的信息(不常用)
  • trace :用于调用请求消息的远程应用程序层回路(不常用)

请求

请求格式

以上所有操作都支持以下元数据字段

字段 必填 详情 示例
path 追加到 base URL的路径。 用于访问特定的URI "/1234", "/search?lastName=Jones"
Headers* 任何第一字母为大写字母的字段均作为请求头发送 "Content-Type", "Accept"

检索数据

要从 HTTP 终结点检索数据,请使用 GET 方法和以下 JSON 的 HTTP 绑定:

{
  "operation": "get"
}

可以指定 URI :

{
  "operation": "get",
  "metadata": {
    "path": "/things/1234"
  }
}

响应

响应正文包含 HTTP 终结点返回的数据。 data 字段包含一个 HTTP 响应实体作为字节数组(通过curl Base64 编码). metadata 字段含有:

字段 必填 详情 示例
statusCode HTTP 状态代码 200, 404, 503
status 状态说明 "200 OK", "201 Created"
Headers* 任何第一字母为大写字母的字段均作为请求头发送 "Content-Type"

示例

请求 base URL


curl -d "{ \"operation\": \"get\" }" \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "get" }' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

请求特定路径


curl -d "{ \"operation\": \"get\", \"metadata\": { \"path\": \"/things/1234\" } }" \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "get", "metadata": { "path": "/things/1234" } }' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

发送和更新数据

要将数据发送到 HTTP 终结点,请调用带有 POST的 HTTP 绑定, PUT,或 PATCH 方法和以下 JSON 正文:

{
  "operation": "post",
  "data": "content (default is JSON)",
  "metadata": {
    "path": "/things",
    "Content-Type": "application/json; charset=utf-8"
  }
}

示例

发布新记录


curl -d "{ \"operation\": \"post\", \"data\": \"YOUR_BASE_64_CONTENT\", \"metadata\": { \"path\": \"/things\" } }" \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

curl -d '{ "operation": "post", "data": "YOUR_BASE_64_CONTENT", "metadata": { "path": "/things" } }' \
      http://localhost:<dapr-port>/v1.0/bindings/<binding-name>

相关链接