Skip to content

Commit 9707baf

Browse files
committed
Merge branch 'feat/httpd_uri_handler_doc_update' into 'master'
feat(esp_http_server): Added httpd URI handler docs See merge request espressif/esp-idf!37560
2 parents 7daa7c8 + 42e49ea commit 9707baf

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

docs/en/api-reference/protocols/esp_http_server.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,41 @@ RESTful API
106106

107107
:example:`protocols/http_server/restful_server` demonstrates how to implement a RESTful API server and HTTP server, with a frontend browser UI, and designs several APIs to fetch resources, using mDNS to parse the domain name, and deploying the webpage to host PC via semihost technology or to SPI flash or SD Card.
108108

109+
URI Handlers
110+
------------
111+
112+
The HTTP server allows you to register URI handlers to handle different HTTP requests. Each URI handler is associated with a specific URI and HTTP method (GET, POST, etc.). The handler function is called whenever a request matching the URI and method is received.
113+
114+
The handler function should return an :cpp:type:`esp_err_t` value.
115+
116+
.. code-block:: c
117+
118+
esp_err_t my_uri_handler(httpd_req_t *req)
119+
{
120+
// Handle the request
121+
// ...
122+
123+
// Return ESP_OK if the request was handled successfully
124+
return ESP_OK;
125+
126+
// Return an error code to close the connection
127+
// return 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+
In this example, the `my_uri_handler` function handles requests to the `/my_uri` URI. If the handler returns :c:macro:`ESP_OK`, the connection remains open. If it returns any other value, the connection is closed. This behavior allows the application to manage connection closure based on specific events or conditions.
143+
109144
API Reference
110145
-------------
111146

docs/zh_CN/api-reference/protocols/esp_http_server.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
109144
API 参考
110145
--------
111146

0 commit comments

Comments
 (0)