Message Rest API QuickStart

Set up Message Rest API

Before using Rest API, you need to complete the following setup steps.

Step 1: Create a service account

Note

Currently, you can't edit a service account. If you need a service account to have Super Admin access, make sure to enable it when creating the service account. By default, service accounts do not have Super Admin enabled.

To create a service account, follow these steps.

  1. On the left navigation pane, click Service Accounts.

  2. Click Create Service Account.

  3. (Optional) Select Super Admin to grant the service account with Super admin access to a namespace or tenant.

  4. Enter a name for the service account, and then click Confirm.

Step 2: Generate a new token for the service account

Note

  • Before getting the token of a service account, verify that the service account is authorized as a superuser or an admin of the tenants and namespaces.
  • A token has a system-defined Time-To-Live (TTL) of 7 days. Before a token expires, ensure that you generate a new token for your service account.

To get a token using the StreamNative Console, follow these steps.

  1. On the left navigation pane, click Service Accounts.

  2. In the row of the service account you want to use, in the Token column, click Generate new token, then click the Copy icon to copy the token to your clipboard.

Step 3: Grant service account permissions

Note

If you use a Super Admin service account, you can skip this step because a Super Admin service account has the required permissions already.

  1. On the left navigation pane, in the Admin section, click Tenants/Namespaces.

  2. Select the Public tenant, then select the Default namespace under the tenant.

  3. Select the POLICY tab.

  4. In the Authorization area, click ADD ROLE, and select the name of the service account you just created in the previous section.

  5. In the Authorization area, on the drop-down menu below the service name you just added, select the consume and produce roles. The roles are added to your service account.

Step 4: Get the HTTP Service URL of your Pulsar cluster

To get the service URL of a Pulsar cluster through the StreamNative Console, follow these steps.

  1. On the left navigation pane, in the Admin area, click Pulsar Clusters.

  2. Select the Details tab, and in the Access Points area, click Copy at the end of the row of the service URL.

Work with Message Rest API

Note

Rest API does not validate the data schema and directly stores the accepted binary data into a topic. You should ensure the correctness of the data schema to ensure compatibility with the Pulsar client in other languages.

After you have completed the configuration steps above, you can use Rest API to produce and consume messages. Rest API sends binary data. You can use the cURL tool to encode a string (for example Hi Pulsar) into UTF-8 format bytes. To send bytes in another format, you need to specify an appropriate file. For more information, see the cURL documentation.

  1. Create a subscription on the topic.

    curl -X PUT https://<endpoint>:<port>/admin/v2/persistent/public/default/rest-topic/subscription/rest-sub \
      --header 'Authorization: Bearer <Access Token>' \
      --header 'Accept: application/json' \
      --header 'Content-Type: application/json'
    

    You should see the following output:

    # No content response
    
  2. Produce messages to the topic.

    curl -X POST https://<endpoint>:<port>/admin/rest/topics/v1/persistent/public/default/rest-topic/message \
      --header 'Authorization: Bearer <Access Token>' \
      --header 'Accept: application/json' \
      --header 'Content-Type: application/octet-stream' \
      --data-binary 'Hi, Pulsar'
    

    You should see the following output:

    # Message id in string format
    10:0:-1:0
    
  3. Consume messages from the topic.

    curl -X POST https://<endpoint>:<port>/admin/rest/topics/v1/persistent/public/default/rest-topic/rest-sub/message \
      --header 'Authorization: Bearer <Access Token>' \
      --header 'Accept: application/octet-stream' \
      --header 'Content-Type: application/json' -v
    

    You should see the following output:

    # Headers
    X-Pulsar-Message-Id: CAoQACAAMAE=
    X-Pulsar-Message-String-Id: 10:0:-1:0
    X-Pulsar-Sequence-Id: 0
    
    # Body
    Hi, Pulsar
    
  4. Acknowledge messages.

    curl -X PUT https://<endpoint>:<port>/admin/rest/topics/v1/persistent/public/default/rest-topic/rest-sub/message \
      --header 'Authorization: Bearer <Access Token>' \
      --header 'Accept: application/json' \
      --header 'Content-Type: application/json' \
      --data-raw 'CAoQACAAMAE='
    

    You should see the following output:

    # No content response
    

Message Rest API reference

For further information, see Message Rest API reference.

Previous
Websocket