File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
docs/zh_CN/api-reference/protocols Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -106,6 +106,41 @@ RESTful API
106106
107107:example: `protocols/http_server/restful_server ` 演示了如何实现 RESTful API 服务器和 HTTP 服务器,并结合前端浏览器 UI,设计了多个 API 来获取资源,使用 mDNS 解析域名,并通过半主机技术将网页部署到主机 PC、SPI flash 或 SD 卡上。
108108
109+ URI 处理程序
110+ ------------
111+
112+ HTTP 服务器可以注册 URI 处理程序以处理不同的 HTTP 请求。每个 URI 处理程序都与特定的 URI 和 HTTP 方法(如 GET、POST 等)相关联。当接收到与 URI 和 HTTP 方法相匹配的请求时,会调用相应的处理程序函数。
113+
114+ 处理程序函数应返回 :cpp:type: `esp_err_t ` 值。
115+
116+ .. code-block :: c
117+
118+ esp_err_t my_uri_handler(httpd_req_t *req)
119+ {
120+ // 处理请求
121+ // ……
122+
123+ // 如果请求处理成功,则返回 ESP_OK
124+ return ESP_OK;
125+
126+ // 返回错误代码以关闭连接
127+ // 返回 ESP_FAIL
128+ }
129+
130+ void register_uri_handlers(httpd_handle_t server)
131+ {
132+ httpd_uri_t my_uri = {
133+ .uri = "/my_uri",
134+ .method = HTTP_GET,
135+ .handler = my_uri_handler,
136+ .user_ctx = NULL
137+ };
138+
139+ httpd_register_uri_handler(server, &my_uri);
140+ }
141+
142+ 在此示例中,`my_uri_handler ` 函数用于处理对 `/my_uri ` URI 的请求。如果处理程序返回 :c:macro: `ESP_OK `,则连接保持打开状态。如果返回其他值,则连接关闭。因此,应用程序可以根据特定事件或条件来管理连接的状态。
143+
109144API 参考
110145--------
111146
You can’t perform that action at this time.
0 commit comments