Cloudflare 中文文档
Queues
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Publish to a Queue via HTTP

Publish to a Queue directly via HTTP and Workers.

The following example shows you how to publish messages to a queue from any HTTP client, using a shared secret to securely authenticate the client.

This allows you to write to a Queue from any service or programming language that support HTTP, including Go, Rust, Python or even a Bash script.

​​ Prerequisites

Configure your wrangler.toml file as follows:

​​ 1. Create a shared secret

Before you deploy the Worker, you need to create a secret that you can use as a shared secret. A shared secret is a secret that both the client uses to authenticate and the server (your Worker) matches against for authentication.

To generate a cryptographically secure secret, you can use the openssl command-line tool and wrangler secret to create a hex-encoded string that can be used as the shared secret:

Copy this string and paste it into the prompt for wrangler secret:

This secret will also need to be used by the client application writing to the queue: ensure you store it securely.

​​ 2. Create the Worker

The following Worker script:

  1. Authenticates the client using a shared secret.
  2. Validates that the payload uses JSON.
  3. Publishes the payload to the queue.

To deploy this Worker:

​​ 3. Send a test message

To make sure you successfully authenticate and write a message to your queue, use curl on the command line:

This will issue a HTTP POST request, and if successful, return a HTTP 200 with a success: true response body.

  • If you receive a HTTP 403, this is because the Authorization header is invalid, or you did not configure a secret.
  • If you receive a HTTP 500, this is either because you did not correctly create a shared secret to your Worker, or you attempted to send an invalid message to your queue.

You can use wrangler tail to debug the output of console.log.