webclient_post_sample.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * File : webclient_post_sample.c
  3. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2018-08-03 chenyong the first version
  22. */
  23. #include <string.h>
  24. #include <rtthread.h>
  25. #include <webclient.h>
  26. #define POST_RESP_BUFSZ 1024
  27. #define POST_HEADER_BUFSZ 1024
  28. #define POST_LOCAL_URI "http://www.rt-thread.com/service/echo"
  29. const char *post_data = "RT-Thread is an open source IoT operating system from China!";
  30. int webclient_post_test(int argc, char **argv)
  31. {
  32. struct webclient_session* session = RT_NULL;
  33. unsigned char *buffer = RT_NULL;
  34. char *URI = RT_NULL;
  35. int index, ret = 0;
  36. int bytes_read, resp_status;
  37. if (argc == 1)
  38. {
  39. URI = web_strdup(POST_LOCAL_URI);
  40. if(URI == RT_NULL)
  41. {
  42. rt_kprintf("no memory for create URI buffer.\n");
  43. return -1;
  44. }
  45. }
  46. else if (argc == 2)
  47. {
  48. URI = web_strdup(argv[1]);
  49. if(URI == RT_NULL)
  50. {
  51. rt_kprintf("no memory for create URI buffer.\n");
  52. return -1;
  53. }
  54. }
  55. else
  56. {
  57. rt_kprintf("webclient_post_test [URI] - webclient POST request test.\n");
  58. return -1;
  59. }
  60. buffer = (unsigned char *) web_malloc(POST_RESP_BUFSZ);
  61. if (buffer == RT_NULL)
  62. {
  63. rt_kprintf("no memory for receive response buffer.\n");
  64. ret = -RT_ENOMEM;
  65. goto __exit;
  66. }
  67. /* create webclient session and set header response size */
  68. session = webclient_session_create(POST_HEADER_BUFSZ);
  69. if (session == RT_NULL)
  70. {
  71. ret = -RT_ENOMEM;
  72. goto __exit;
  73. }
  74. /* build header for upload */
  75. webclient_header_fields_add(session, "Content-Length: %d\r\n", strlen(post_data));
  76. webclient_header_fields_add(session, "Content-Type: application/octet-stream\r\n");
  77. /* send POST request by default header */
  78. if ((resp_status = webclient_post(session, URI, post_data)) != 200)
  79. {
  80. rt_kprintf("webclient POST request failed, response(%d) error.\n", resp_status);
  81. ret = -RT_ERROR;
  82. goto __exit;
  83. }
  84. LOG_I("webclient POST request response data :");
  85. do
  86. {
  87. bytes_read = webclient_read(session, buffer, POST_RESP_BUFSZ);
  88. if (bytes_read <= 0)
  89. {
  90. break;
  91. }
  92. for (index = 0; index < bytes_read; index++)
  93. {
  94. rt_kprintf("%c", buffer[index]);
  95. }
  96. } while (1);
  97. rt_kprintf("\n");
  98. __exit:
  99. if (session)
  100. {
  101. webclient_close(session);
  102. }
  103. if (buffer)
  104. {
  105. web_free(buffer);
  106. }
  107. if (URI)
  108. {
  109. web_free(URI);
  110. }
  111. return ret;
  112. }
  113. #ifdef FINSH_USING_MSH
  114. #include <finsh.h>
  115. MSH_CMD_EXPORT_ALIAS(webclient_post_test, web_post_test, webclient_post_test [URI] - webclient POST request test.);
  116. #endif /* FINSH_USING_MSH */