http-api.md 8.1 KB
Newer Older
如梦技术's avatar
如梦技术 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# Http Api 接口

### HTTP 状态码 (status codes)

接口在调用成功时总是返回 200 OK,响应内容则以 JSON 格式返回。

可能的状态码如下:

| Status Code | Description                                              |
| ----------- | -------------------------------------------------------- |
| 200         | 成功,返回的 JSON 数据将提供更多信息                     |
| 400         | 客户端请求无效,例如请求体或参数错误                     |
| 401         | 客户端未通过服务端认证,使用无效的身份验证凭据可能会发生 |
| 404         | 找不到请求的路径或者请求的对象不存在                     |
| 405         | 请求方法错误                     						 |
| 500         | 服务端处理请求时发生内部错误                             |

### 返回码 (result codes)

接口的响应消息体为 JSON 格式,其中总是包含返回码 `code`

可能的返回码如下:

| Return Code | Description                |
| ----------- | -------------------------- |
| 1           | 成功                       |
| 101         | 关键请求参数缺失           |
| 102         | 请求参数错误               |
| 103         | 用户名或密码错误           |
| 104         | 请求方法错误               |
| 105         | 未知错误                   |

浅梦2013's avatar
浅梦2013 已提交
33

如梦技术's avatar
如梦技术 已提交
34 35 36 37 38 39 40 41 42 43 44

## 消息发布

### POST /api/v1/mqtt/publish

发布 MQTT 消息。

**Parameters (json):**

| Name     | Type    | Required | Default | Description                                                 |
| -------- | ------- | -------- | ------- | ----------------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
45 46 47
| topic    | String  | Required |         | 主题                                                         |
| clientId | String  | Required |         | 客户端标识符                                                  |
| payload  | String  | Required |         | 消息正文                                                      |
如梦技术's avatar
如梦技术 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60
| encoding | String  | Optional | plain   | 消息正文使用的编码方式,目前仅支持 目前仅支持 `plain``hex``base64` |
| qos      | Integer | Optional | 0       | QoS 等级                                                    |
| retain   | Boolean | Optional | false   | 是否为保留消息                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

```bash
如梦技术's avatar
如梦技术 已提交
61
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/publish" -d '{"topic":"a/b/c","payload":"Hello World","qos":1,"retain":false,"clientId":"example"}'
如梦技术's avatar
如梦技术 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75

{"code":0}
```

## 主题订阅

### POST /api/v1/mqtt/subscribe

订阅 MQTT 主题。

**Parameters (json):**

| Name     | Type    | Required | Default | Description                                           |
| -------- | ------- | -------- | ------- | ----------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
76 77
| topic    | String  | Required |         | 主题                                                  |
| clientId | String  | Required |         | 客户端标识符                                          |
如梦技术's avatar
如梦技术 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90
| qos      | Integer | Optional | 0       | QoS 等级                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

同时订阅 `a`, `b`, `c` 三个主题

```bash
如梦技术's avatar
如梦技术 已提交
91
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/subscribe" -d '{"topic":"a/b/c","qos":1,"clientId":"example"}'
如梦技术's avatar
如梦技术 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104

{"code":0}
```

### POST /api/v1/mqtt/unsubscribe

取消订阅。

**Parameters (json):**

| Name     | Type   | Required | Default | Description  |
| -------- | ------ | -------- | ------- | ------------ |
| topic    | String | Required |         | 主题         |
如梦技术's avatar
如梦技术 已提交
105
| clientId | String | Required |         | 客户端标识符 |
如梦技术's avatar
如梦技术 已提交
106 107 108 109 110 111 112 113 114 115 116 117

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

取消订阅 `a` 主题

```bash
118
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/unsubscribe" -d '{"topic":"a","clientId":"example"}'
如梦技术's avatar
如梦技术 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132

{"code":0}
```

## 消息批量发布

### POST /api/v1/mqtt/publish/batch

批量发布 MQTT 消息。

**Parameters (json):**

| Name         | Type    | Required | Default | Description                                                 |
| ------------ | ------- | -------- | ------- | ----------------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
133 134
| [0].topic    | String  | Required |         | 主题                                                         |
| [0].clientId | String  | Required |         | 客户端标识符                                                |
如梦技术's avatar
如梦技术 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
| [0].payload  | String  | Required |         | 消息正文                                                    |
| [0].encoding | String  | Optional | plain   | 消息正文使用的编码方式,目前仅支持 `plain``hex``base64` |
| [0].qos      | Integer | Optional | 0       | QoS 等级                                                    |
| [0].retain   | Boolean | Optional | false   | 是否为保留消息                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

```bash
如梦技术's avatar
如梦技术 已提交
149
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/publish/batch" -d '[{"topic":"a/b/c","payload":"Hello World","qos":1,"retain":false,"clientId":"example"},{"topic":"a/b/c","payload":"Hello World Again","qos":0,"retain":false,"clientId":"example"}]'
如梦技术's avatar
如梦技术 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163

{"code":0}
```

## 主题批量订阅

### POST /api/v1/mqtt/subscribe/batch

批量订阅 MQTT 主题。

**Parameters (json):**

| Name         | Type    | Required | Default | Description                                           |
| ------------ | ------- | -------- | ------- | ----------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
164 165
| [0].topic    | String  | Required |         | 主题                                                  |
| [0].clientId | String  | Required |         | 客户端标识符                                          |
如梦技术's avatar
如梦技术 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
| [0].qos      | Integer | Optional | 0       | QoS 等级                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

一次性订阅 `a`, `b`, `c` 三个主题

```bash
如梦技术's avatar
如梦技术 已提交
179
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/subscribe/batch" -d '[{"topic":"a","qos":1,"clientId":"example"},{"topic":"b","qos":1,"clientId":"example"},{"topic":"c","qos":1,"clientId":"example"}]'
如梦技术's avatar
如梦技术 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192

{"code":0}
```

### POST /api/v1/mqtt/unsubscribe/batch

批量取消订阅。

**Parameters (json):**

| Name         | Type   | Required | Default | Description  |
| ------------ | ------ | -------- | ------- | ------------ |
| [0].topic    | String | Required |         | 主题         |
如梦技术's avatar
如梦技术 已提交
193
| [0].clientId | String | Required |         | 客户端标识符 |
如梦技术's avatar
如梦技术 已提交
194 195 196 197 198 199 200 201 202 203 204 205

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

一次性取消订阅 `a`, `b` 主题

```bash
206
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/unsubscribe/batch" -d '[{"topic":"a","clientId":"example"},{"topic":"b","clientId":"example"}]'
如梦技术's avatar
如梦技术 已提交
207 208 209

{"code":0}
```
浅梦2013's avatar
浅梦2013 已提交
210 211 212

## 踢除指定客户端

213
### POST /api/v1/clients/delete
浅梦2013's avatar
浅梦2013 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

踢除指定客户端。注意踢除客户端操作会将连接与会话一并终结。

**Query Parameters:**

| Name     | Type   | Required | Description |
| -------- | ------ | -------- | ----------- |
| clientId | String | True     | ClientID    |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

踢除指定客户端

```bash
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/clients/delete?clientId=123"

{"code":0}
```