Skip to content

Commit df70e20

Browse files
committed
How to HTTP Publish
1 parent aeb2f40 commit df70e20

File tree

3 files changed

+417
-0
lines changed

3 files changed

+417
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: How to Publish MQTT Messages via HTTP with Azure Event Grid
3+
description: Explains how to publish MQTT messages via HTTP with Azure Event Grid for scalable server-to-device communication. Learn how to use the HTTP Publish API effectively.
4+
#customer intent: As a backend developer, I want to publish MQTT messages via HTTP so that I can integrate with Azure Event Grid without maintaining persistent MQTT sessions.
5+
author: spelluru
6+
contributors: null
7+
ms.topic: how-to
8+
ms.date: 07/30/2025
9+
ms.author: spelluru
10+
ms.reviewer: spelluru
11+
ms.custom:
12+
- ai-gen-docs-bap
13+
- ai-gen-title
14+
- ai-seo-date:07/30/2025
15+
- ai-gen-description
16+
---
17+
18+
# 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.
20+
21+
This article explains how to publish MQTT messages via HTTP with Azure Event Grid.
22+
23+
## Get Your Connection Details
24+
25+
- Namespace fully qualified domain name (FQDN). Example: `contoso.westus3-1.ts.eventgrid.azure.net`.
26+
- Topic. Example: `devices/CXa-23112/prompt`.
27+
- Entra ID client credentials.
28+
29+
## Get a bearer token
30+
Run the following Azure CLI command to get a bearer token.
31+
32+
```bash
33+
az account get-access-token --resource=https://<namespace> --query accessToken -o tsv
34+
```
35+
36+
## Import to Bruno
37+
Use a tool such as Bruno to
38+
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
48+
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
54+
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
80+
```
81+
82+
Save this token to use in the Authorization: Bearer `<TOKEN>` header.
83+
84+
### Step 3: Prepare Your HTTP POST Request
85+
86+
Here’s an example using curl to simulate the HTTP Publish.
87+
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
101+
102+
Use **MQTTX** or any MQTT library (like paho-mqtt Python) to subscribe to the same topic to confirm delivery.
103+
104+
#### Example MQTTX Steps
105+
106+
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
121+
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.
133+
134+
💡 Check logs in Event Grid Metrics and Diagnostic Logs for delivery status.
135+
136+
## Option 2: Using Postman or Bruno
137+
138+
This helps you test the HTTP Publish feature using:
139+
140+
- Postman or Bruno collection (provided).
141+
- MQTTX or your MQTT client to verify delivery.
142+
- Entra ID for secure auth.
143+

0 commit comments

Comments
 (0)