Skip to content

Commit 299b4c1

Browse files
committed
Added cURL section
1 parent df70e20 commit 299b4c1

File tree

1 file changed

+45
-92
lines changed

1 file changed

+45
-92
lines changed

articles/event-grid/mqtt-how-to-http-publish.md

Lines changed: 45 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ms.custom:
1616
---
1717

1818
# How to Publish MQTT Messages via HTTP with Azure Event Grid
19-
Azure Event Grid now supports publishing MQTT messages via HTTP, enabling backend systems to send messages to devices without maintaining persistent MQTT connections. This approach simplifies integration for applications that prefer stateless communication, leverages secure authentication with Entra ID, and provides scalable, reliable delivery to MQTT clients. In this article, you'll learn how to use the HTTP Publish API, obtain the necessary credentials, and verify message delivery using popular tools like Postman, Bruno, and MQTTX.
19+
Azure Event Grid now supports publishing MQTT messages via HTTP, enabling backend systems to send messages to devices without maintaining persistent MQTT connections. This approach simplifies integration for applications that prefer stateless communication, uses secure authentication with Entra ID, and provides scalable, reliable delivery to MQTT clients. In this article, you learn how to use the HTTP Publish API, obtain the necessary credentials, and verify message delivery using popular tools like Bruno, and MQTTX.
2020

2121
This article explains how to publish MQTT messages via HTTP with Azure Event Grid.
2222

@@ -30,114 +30,67 @@ This article explains how to publish MQTT messages via HTTP with Azure Event Gri
3030
Run the following Azure CLI command to get a bearer token.
3131

3232
```bash
33-
az account get-access-token --resource=https://<namespace> --query accessToken -o tsv
33+
az account get-access-token --resource=https://<namespaceFQDN> --query accessToken -o tsv
3434
```
3535

36-
## Import to Bruno
37-
Use a tool such as Bruno to
36+
Save this token to use in the `Authorization: Bearer <TOKEN>` header.
3837

39-
1. Open Bruno.
40-
1. Select **Import Collection**, and then select `EventGrid_HTTP_Publish_Postman_Collection.json`.
41-
1. Go to **Variables** tab:
42-
- Replace `{{namespace}}` with your namespace FQDN.
43-
- Replace `{{topic}}` with your MQTT topic.
44-
- Replace `{{entra_token}}` with your token from Step 2.
45-
1. Select **Send** — you should get 202 or 204.
46-
47-
## Step 4: Verify in MQTTX
38+
# [cURL](#tab/curl)
4839

49-
- Open MQTTX and connect using your broker’s endpoint, TLS, and your normal MQTT auth.
50-
- Subscribe to the topic you used in the HTTP POST.
51-
- You should see your payload appear.
52-
53-
## Troubleshoot
40+
Here’s an example cURL command to simulate the HTTP Publish.
5441

55-
- **401 Unauthorized?** — Refresh your token.
56-
- **403 Forbidden?** — Check your topic or permissions.
57-
- **Message doesn’t appear?** — Ensure topic is percent-encoded in the URL, check broker routing config, and verify you’re using the same namespace.
58-
59-
60-
# Tutorials:
61-
62-
## Option 1: Using cURL Command
63-
64-
### Step 1: Get Your Connection Details
65-
66-
- **Event Grid Namespace**: e.g., contoso.westus3-1.ts.eventgrid.azure.net
67-
- **Topic**: e.g., devices/CXa-23112/prompt
68-
- **Entra Client ID** and Tenant: for getting your bearer token.
69-
- **Audience (Resource)**: your Event Grid Namespace URL.
70-
71-
### Step 2: Get an Entra ID Token
72-
73-
Use **Azure CLI** or your preferred flow to get a token:
74-
75-
```bash
76-
az account get-access-token \
77-
--resource=https://contoso.westus3-1.ts.eventgrid.azure.net \
78-
--query accessToken \
79-
-o tsv
42+
```http
43+
curl -X POST "https://contoso.westus3-1.ts.eventgrid.azure.net/mqtt/messages?topic=devices%2XXXX-0000%2Fprompt&api-version=2025-02-15-preview" \
44+
-H "Authorization: Bearer <ENTRA_TOKEN_HERE>" \
45+
-H "mqtt-qos: 1" \
46+
-H "mqtt-retain: 0" \
47+
-H "mqtt-response-topic: devices%2XXXX-00000%2Freply" \
48+
-H "mqtt-correlation-data: XXXXXXX" \
49+
-H "mqtt-user-properties: XXXXXXXXXXXX" \
50+
-H "Content-Type: text/plain;charset=UTF-8" \
51+
--data-raw "Please accept terms of licensing and agreement"
8052
```
8153

82-
Save this token to use in the Authorization: Bearer `<TOKEN>` header.
54+
In this sample command:
8355

84-
### Step 3: Prepare Your HTTP POST Request
56+
- Topic is percent-encoded.
57+
- Add optional headers for QoS, retain flag, response topic, user properties.
58+
- Payload goes in the request body.
8559

86-
Here’s an example using curl to simulate the HTTP Publish.
60+
# [Bruno](#tab/brno)
8761

88-
```bash
89-
curl -X POST "https://contoso.westus3-1.ts.eventgrid.azure.net/mqtt/messages?topic=devices%2FCXa-23112%2Fprompt&api-version=2025-02-15-preview" \
90-
-H "Authorization: Bearer <ENTRA_TOKEN_HERE>" \
91-
-H "mqtt-qos: 1" \
92-
-H "mqtt-retain: 0" \
93-
-H "mqtt-response-topic: devices%2FCXa-23112%2Freply" \
94-
-H "mqtt-correlation-data: PlXCscK2wrbCuy8=" \
95-
-H "mqtt-user-properties: W3siVXJnZW5jeSI6ImFsZXJ0In0seyJSZXF1ZXN0SWQiOiI1NWY0YTdlZS1iMGI0LTRkN2YtOGViNS0yZWRiYTJjZWQ1ZDcifV0=" \
96-
-H "Content-Type: text/plain;charset=UTF-8" \
97-
--data-raw "Please accept terms of licensing and agreement"
98-
```
99-
100-
### Step 4: Verify Publish with MQTT Client
62+
## Import to Bruno
63+
1. Open Bruno.
64+
1. Select **Import Collection**, and then select `EventGrid_HTTP_Publish_Postman_Collection.json`.
65+
1. Go to **Variables** tab:
66+
- Replace `{{namespace}}` with your namespace FQDN.
67+
- Replace `{{topic}}` with your MQTT topic.
68+
- Replace `{{entra_token}}` with your token from Step 2.
69+
1. Select **Send**. You should get 202 or 204.
10170

102-
Use **MQTTX** or any MQTT library (like paho-mqtt Python) to subscribe to the same topic to confirm delivery.
71+
---
10372

104-
#### Example MQTTX Steps
73+
## Step 4: Verify in MQTTX
74+
Use MQTTX or any MQTT library (like paho-mqtt Python) to subscribe to the same topic to confirm delivery.
10575

10676
1. Create a new connection in MQTTX:
107-
- Host: contoso.westus3-1.ts.eventgrid.azure.net
108-
- Port: 8883 (TLS)
109-
- Client ID: same as your Entra Object ID.
110-
- Username/Password: N/A — use certificate or token auth if configured.
111-
2. Subscribe to devices/CXa-23112/prompt.
112-
3. Run the HTTP Publish and watch for the message in MQTTX.
113-
114-
### Step 5: Validate Success
115-
116-
- If the publish succeeds, you’ll see:
117-
- **HTTP Response**: 204 No Content or 202 Accepted (depending on routing rules).
118-
- MQTT client sees the message instantly.
119-
120-
#### Expected log in your MQTT client
77+
1. `Host: contoso.westus3-1.ts.eventgrid.azure.net`
78+
1. `Port: 8883 (TLS)`
79+
1. `Client ID: same as your Entra Object ID`
80+
1. `Username/Password: N/A — use certificate or token auth if configured`
81+
- Subscribe to the topic you used in the HTTP POST command.
82+
- Run the **HTTP Publish** and watch for the message in MQTTX. You should see your payload appear.
12183

122-
```text
123-
Topic: devices/CXa-23112/prompt
124-
Payload: Please accept terms of licensing and agreement
125-
QoS: 1
126-
```
127-
128-
### Step 6: Handle Failures
129-
130-
- If the token is missing or expired: You’ll get 401 Unauthorized.
131-
- If the topic is invalid or you don’t have rights: 403 Forbidden.
132-
- If routing fails internally: 500 Internal Server Error with diagnostic info.
84+
If the publish **succeeds**, you see:
13385

134-
💡 Check logs in Event Grid Metrics and Diagnostic Logs for delivery status.
86+
- **HTTP Response**: 204 No Content or 202 Accepted (depending on routing rules).
87+
- MQTT client sees the message instantly.
13588

136-
## Option 2: Using Postman or Bruno
89+
## Troubleshoot
13790

138-
This helps you test the HTTP Publish feature using:
91+
- **401 Unauthorized?** — If the token is missing or expired: You get 401 Unauthorized. Refresh your token.
92+
- **403 Forbidden?** — If the topic is invalid or you don’t have rights: 403 Forbidden. Check your topic or permissions.
93+
- **500 Internal Server Error** - If routing fails internally, check metrics and diagnostic Logs for your Event Grid namespace.
94+
- **Message doesn’t appear?** — Ensure topic is percent-encoded in the URL, check broker routing config, and verify you’re using the same namespace.
13995

140-
- Postman or Bruno collection (provided).
141-
- MQTTX or your MQTT client to verify delivery.
142-
- Entra ID for secure auth.
14396

0 commit comments

Comments
 (0)