diff --git a/README.md b/README.md index 0545eb3..8e4f1f5 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ WebClient 软件包遵循 Apache-2.0 许可,详见 LICENSE 文件。 RT-Thread online packages IoT - internet of things ---> [*] WebClient: A HTTP/HTTPS Client for RT-Thread + [ ] Enable debug log output [ ] Enable webclient GET/POST samples Select TLS mode (Not support) ---> (x) Not support diff --git a/docs/api.md b/docs/api.md index b2cb5a8..5cfcfcc 100644 --- a/docs/api.md +++ b/docs/api.md @@ -202,6 +202,23 @@ int webclient_request(const char *URI, const char *header, const char *post_data | `>0` | 成功接收数据的长度 | | <=0 | 接收数据失败 | +## 拼接请求头部数据 + +```c +int webclient_request_header_add(char **request_header, const char *fmt, ...); +``` + +该函数适用于 webclient_request 函数发送请求之前,头部数据的拼接和添加。 + +| 参数 | 描述 | +|:------------------|:-----------------------------------| +|request_header | 请求头部数据缓冲区地址 | +|fmt | 添加字段数据的表达式 | +|... | 添加的字段数据,为可变参数 | +| **返回** | **描述** | +| `>0` | 成功添加的字段数据的长度 | +| <=0 | 头部数据添加失败或内存不足 | + ## 获取 HTTP 响应状态码 diff --git a/docs/samples.md b/docs/samples.md index 0fe508c..41eaf3b 100644 --- a/docs/samples.md +++ b/docs/samples.md @@ -23,8 +23,9 @@ WebClient 软件包提供两个 HTTP Client 示例程序, 分别用于演示软 RT-Thread online packages IoT - internet of things ---> [*] WebClient: A HTTP/HTTPS Client for RT-Thread - [ ] Enable support tls protocol + [ ] Enable debug log output [*] Enable webclient GET/POST samples # 开启 WebClient 测试例程 + Select TLS mode (Not support) ---> Version (latest) ---> # 开启使用最新版本软件包 ``` @@ -50,17 +51,22 @@ GET 请求示例流程: GET 请求示例使用方式有如下两种: -- 在 MSH 中使用命令 `web_get_test` 执行 GET 请求示例程序,可以获取并打印显示默认网址下载的文件信息,如下图 LOG 显示: +- 在 MSH 中使用命令 `web_get_test` 执行 GET 请求示例程序,可以获取并打印显示默认网址下载的文件信息;在 MSH 中使用命令 `web_get_test -s` 执行 POST 请求示例程序,使用简化接口(webclient_request 接口)发送 GET请求,适用于简短数据的收发。如下图 LOG 显示: ```c -msh />web_get_test -webclient GET request response data : +msh />web_get_test +webclient get response data: +RT-Thread is an open source IoT operating system from China, which has strong scalability: from a tiny kernel running on a tiny core, for example ARM Cortex-M0, or Cortex-M3/4/7, to a rich feature system running on MIPS32, ARM Cortex-A8, ARM Cortex-A9 DualCore etc. + +msh />web_get_test -s +webclient send get request by simplify request interface. +webclient get response data: RT-Thread is an open source IoT operating system from China, which has strong scalability: from a tiny kernel running on a tiny core, for example ARM Cortex-M0, or Cortex-M3/4/7, to a rich feature system running on MIPS32, ARM Cortex-A8, ARM Cortex-A9 DualCore etc. msh /> ``` -- 在 MSH 中使用命令 `web_get_test [URI]` 格式命令执行 GET 请求示例程序,其中 URI 为用户自定义的支持 GET 请求的地址。 +- 在 MSH 中使用命令 `web_get_test [URI]` 或 `web_get_test -s [URI]` 格式命令执行 GET 请求示例程序,其中 URI 为用户自定义的支持 GET 请求的地址。 ### POST 请求示例 @@ -75,13 +81,18 @@ POST 请求示例流程如下: POST 请求示例使用方式有如下两种: -- 在 MSH 中使用命令 `web_post_test` 执行 POST 请求示例程序,可以获取并打印显示响应数据(默认 POST 请求的地址是类似于回显的地址,会返回上传的数据),如下图 LOG 显示: +- 在 MSH 中使用命令 `web_post_test` 执行 POST 请求示例程序,可以获取并打印显示响应数据(默认 POST 请求的地址是类似于回显的地址,会返回上传的数据);在 MSH 中使用命令 `web_post_test -s` 执行 POST 请求示例程序,使用简化接口(webclient_request 接口)发送 POST 请求,适用于简短数据的收发。如下图 LOG 显示: ```c msh />web_post_test -webclient POST request response data : +webclient post response data : RT-Thread is an open source IoT operating system from China! msh /> +msh />web_post_test -s +webclient send post request by simplify request interface. +webclient post response data: +RT-Thread is an open source IoT operating system from China! +msh /> ``` -- 在 MSH 中使用命令 `web_post_test [URI]` 格式命令执行 POST 请求示例程序,其中 URI 为用户自定义的支持 POST 请求的地址。 \ No newline at end of file +- 在 MSH 中使用命令 `web_post_test [URI]` 或者 `web_post_test -s [URI]` 格式命令执行 POST 请求示例程序,其中 URI 为用户自定义的支持 POST 请求的地址。 \ No newline at end of file diff --git a/docs/user-guide.md b/docs/user-guide.md index 02c10c1..d00980c 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -14,6 +14,7 @@ RT-Thread online packages IoT - internet of things ---> [*] WebClient: A HTTP/HTTPS Client for RT-Thread + [ ] Enable debug log output [ ] Enable webclient GET/POST samples Select TLS mode (Not support) ---> (x) Not support @@ -22,6 +23,8 @@ RT-Thread online packages Version (latest) ---> ``` +**Enable debug log output**:开启调试日志显示,可以用于查看请求和响应的头部数据信息; + **Enable webclient GET/POST samples** :添加示例代码; **Select TLS mode** :配置开启 HTTPS 支持,选择支持的模式; @@ -273,7 +276,10 @@ webclient_close(session); 多用于接收数据长度较小,且头部信息已经拼接给出的 GET 请求。 ```c -char *result; +char *result, *header = RT_NULL; + +/* 拼接自定义头部数据 */ +webclient_request_header_add(&header, "User-Agent: RT-Thread HTTP Agent\r\n"); webclient_request(URI, header, NULL, &result); @@ -341,8 +347,13 @@ webclient_close(session); ```c char *post_data = "abcdefg"; +char *header = RT_NULL; + +/* 拼接自定义头部数据 */ +webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data)); +webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n"); -webclient_request(URI, NULL, post_data, NULL); +webclient_request(URI, header, post_data, NULL); ``` ## 常见问题 @@ -355,7 +366,7 @@ webclient_request(URI, NULL, post_data, NULL); - 原因:使用 HTTPS 地址但是没有开启 HTTPS 支持。 -- 解决方法:在 WebClient 软件包 menuconfig 配置选项中开启 `Enable support tls protocol` 选项支持。 +- 解决方法:在 WebClient 软件包 menuconfig 配置选项中 选择 `Select TLS mode` 选项为 `MbedTLS support` 或者 `SAL TLS support`。 ### 头部数据长度超出