以下示例展示如何使用 Cloudflare API 令牌进行身份验证,从任何 HTTP 客户端向 Queue 发布消息。
这使您可以从任何支持 HTTP 的服务或编程语言(包括 Go、Rust、Python 甚至 Bash 脚本)写入 Queue。
- 通过 Cloudflare 仪表板 ↗ 或 wrangler CLI 创建的队列。
- 具有
Queues Edit权限的 Cloudflare API 令牌。
要确认身份验证成功并将消息写入队列,请在命令行使用 curl:
# Make sure to replace the placeholder with your shared secret
curl -XPOST -H "Authorization: Bearer <paste-your-api-token-here>" "https://api.cloudflare.com/client/v4/accounts/<paste-your-account-id-here>/queues/<paste-your-queue-id-here>/messages" --data '{ "body": { "greeting": "hello" } }'{"success":true}这将发出 HTTP POST 请求,若成功,将返回 HTTP 200 及 success: true 响应正文。
- 若收到 HTTP 403,表示 API 令牌无效或没有
Queues Edit权限。
有关 HTTP Push API 的完整文档,请参阅 Cloudflare API 文档。