webclient_post_sample.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. session = RT_NULL;
  67. }
  68. if (buffer)
  69. {
  70. web_free(buffer);
  71. }
  72. return ret;
  73. }
  74. /* send HTTP POST request by simplify request interface, it used to received shorter data */
  75. static int webclient_post_smpl(const char *uri, const char *post_data, size_t data_len)
  76. {
  77. char *response = RT_NULL;
  78. char *header = RT_NULL;
  79. size_t resp_len = 0;
  80. int index = 0;
  81. webclient_request_header_add(&header, "Content-Length: %d\r\n", strlen(post_data));
  82. webclient_request_header_add(&header, "Content-Type: application/octet-stream\r\n");
  83. if (webclient_request(uri, header, post_data, data_len, (void **)&response, &resp_len) < 0)
  84. {
  85. rt_kprintf("webclient send post request failed.");
  86. web_free(header);
  87. return -RT_ERROR;
  88. }
  89. rt_kprintf("webclient send post request by simplify request interface.\n");
  90. rt_kprintf("webclient post response data: \n");
  91. for (index = 0; index < resp_len; index++)
  92. {
  93. rt_kprintf("%c", response[index]);
  94. }
  95. rt_kprintf("\n");
  96. if (header)
  97. {
  98. web_free(header);
  99. }
  100. if (response)
  101. {
  102. web_free(response);
  103. }
  104. return 0;
  105. }
  106. int webclient_post_test(int argc, char **argv)
  107. {
  108. char *uri = RT_NULL;
  109. if (argc == 1)
  110. {
  111. uri = web_strdup(POST_LOCAL_URI);
  112. if(uri == RT_NULL)
  113. {
  114. rt_kprintf("no memory for create post request uri buffer.\n");
  115. return -RT_ENOMEM;
  116. }
  117. webclient_post_comm(uri, (void *)post_data, strlen(post_data));
  118. }
  119. else if (argc == 2)
  120. {
  121. if (strcmp(argv[1], "-s") == 0)
  122. {
  123. uri = web_strdup(POST_LOCAL_URI);
  124. if(uri == RT_NULL)
  125. {
  126. rt_kprintf("no memory for create post request uri buffer.\n");
  127. return -RT_ENOMEM;
  128. }
  129. webclient_post_smpl(uri, (void *)post_data, strlen(post_data));
  130. }
  131. else
  132. {
  133. uri = web_strdup(argv[1]);
  134. if(uri == RT_NULL)
  135. {
  136. rt_kprintf("no memory for create post request uri buffer.\n");
  137. return -RT_ENOMEM;
  138. }
  139. webclient_post_comm(uri, (void *)post_data, strlen(post_data));
  140. }
  141. }
  142. else if(argc == 3 && strcmp(argv[1], "-s") == 0)
  143. {
  144. uri = web_strdup(argv[2]);
  145. if(uri == RT_NULL)
  146. {
  147. rt_kprintf("no memory for create post request uri buffer.\n");
  148. return -RT_ENOMEM;
  149. }
  150. webclient_post_smpl(uri, (void *)post_data, strlen(post_data));
  151. }
  152. else
  153. {
  154. rt_kprintf("web_post_test [uri] - webclient post request test.\n");
  155. rt_kprintf("web_post_test -s [uri] - webclient simplify post request test.\n");
  156. return -RT_ERROR;
  157. }
  158. if (uri)
  159. {
  160. web_free(uri);
  161. }
  162. return RT_EOK;
  163. }
  164. #ifdef FINSH_USING_MSH
  165. #include <finsh.h>
  166. MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient post request test.);
  167. #endif /* FINSH_USING_MSH */