webclient_post_sample.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-03 chenyong the first version
  9. */
  10. #include <string.h>
  11. #include <rtthread.h>
  12. #include <webclient.h>
  13. #define POST_RESP_BUFSZ 1024
  14. #define POST_HEADER_BUFSZ 1024
  15. #define POST_LOCAL_URI "http://www.rt-thread.com/service/echo"
  16. const char *post_data = "RT-Thread is an open source IoT operating system from China!";
  17. /* send HTTP POST request by common request interface, it used to receive longer data */
  18. static int webclient_post_comm(const char *uri, const void *post_data, size_t data_len)
  19. {
  20. struct webclient_session* session = RT_NULL;
  21. unsigned char *buffer = RT_NULL;
  22. int index, ret = 0;
  23. int bytes_read, resp_status;
  24. buffer = (unsigned char *) web_malloc(POST_RESP_BUFSZ);
  25. if (buffer == RT_NULL)
  26. {
  27. rt_kprintf("no memory for receive response buffer.\n");
  28. ret = -RT_ENOMEM;
  29. goto __exit;
  30. }
  31. /* create webclient session and set header response size */
  32. session = webclient_session_create(POST_HEADER_BUFSZ);
  33. if (session == RT_NULL)
  34. {
  35. ret = -RT_ENOMEM;
  36. goto __exit;
  37. }
  38. /* build header for upload */
  39. webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data));
  40. webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n");
  41. /* send POST request by default header */
  42. if ((resp_status = webclient_post(session, uri, post_data, data_len)) != 200)
  43. {
  44. rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status);
  45. ret = -RT_ERROR;
  46. goto __exit;
  47. }
  48. rt_kprintf("webclient post response data: \n");
  49. do
  50. {
  51. bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ);
  52. if (bytes_read <= 0)
  53. {
  54. break;
  55. }
  56. for (index = 0; index < bytes_read; index++)
  57. {
  58. rt_kprintf("%c", buffer[index]);
  59. }
  60. } while (1);
  61. rt_kprintf("\n");
  62. __exit:
  63. if (session)
  64. {
  65. webclient_close(session);
  66. }
  67. if (buffer)
  68. {
  69. web_free(buffer);
  70. }
  71. return ret;
  72. }
  73. /* send HTTP POST request by simplify request interface, it used to received shorter data */
  74. static int webclient_post_smpl(const char *uri, const char *post_data, size_t data_len)
  75. {
  76. char *response = RT_NULL;
  77. char *header = RT_NULL;
  78. size_t resp_len = 0;
  79. int index = 0;
  80. webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data));
  81. webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n");
  82. if (webclient_request(uri, header, post_data, data_len, (void **)&response, &resp_len) < 0)
  83. {
  84. rt_kprintf("webclient send post request failed.");
  85. web_free(header);
  86. return -RT_ERROR;
  87. }
  88. rt_kprintf("webclient send post request by simplify request interface.\n");
  89. rt_kprintf("webclient post response data: \n");
  90. for (index = 0; index < resp_len; index++)
  91. {
  92. rt_kprintf("%c", response[index]);
  93. }
  94. rt_kprintf("\n");
  95. if (header)
  96. {
  97. web_free(header);
  98. }
  99. if (response)
  100. {
  101. web_free(response);
  102. }
  103. return 0;
  104. }
  105. int webclient_post_test(int argc, char **argv)
  106. {
  107. char *uri = RT_NULL;
  108. if (argc == 1)
  109. {
  110. uri = web_strdup(POST_LOCAL_URI);
  111. if(uri == RT_NULL)
  112. {
  113. rt_kprintf("no memory for create post request uri buffer.\n");
  114. return -RT_ENOMEM;
  115. }
  116. webclient_post_comm(uri, (void *)post_data, strlen(post_data));
  117. }
  118. else if (argc == 2)
  119. {
  120. if (strcmp(argv[1], "-s") == 0)
  121. {
  122. uri = web_strdup(POST_LOCAL_URI);
  123. if(uri == RT_NULL)
  124. {
  125. rt_kprintf("no memory for create post request uri buffer.\n");
  126. return -RT_ENOMEM;
  127. }
  128. webclient_post_smpl(uri, (void *)post_data, strlen(post_data));
  129. }
  130. else
  131. {
  132. uri = web_strdup(argv[1]);
  133. if(uri == RT_NULL)
  134. {
  135. rt_kprintf("no memory for create post request uri buffer.\n");
  136. return -RT_ENOMEM;
  137. }
  138. webclient_post_comm(uri, (void *)post_data, strlen(post_data));
  139. }
  140. }
  141. else if(argc == 3 && strcmp(argv[1], "-s") == 0)
  142. {
  143. uri = web_strdup(argv[2]);
  144. if(uri == RT_NULL)
  145. {
  146. rt_kprintf("no memory for create post request uri buffer.\n");
  147. return -RT_ENOMEM;
  148. }
  149. webclient_post_smpl(uri, (void *)post_data, strlen(post_data));
  150. }
  151. else
  152. {
  153. rt_kprintf("web_post_test [uri] - webclient post request test.\n");
  154. rt_kprintf("web_post_test -s [uri] - webclient simplify post request test.\n");
  155. return -RT_ERROR;
  156. }
  157. if (uri)
  158. {
  159. web_free(uri);
  160. }
  161. return RT_EOK;
  162. }
  163. #ifdef FINSH_USING_MSH
  164. #include <finsh.h>
  165. MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient post request test.);
  166. #endif /* FINSH_USING_MSH */