1
0

http_client_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * @build make examples
  3. *
  4. * @server bin/http_server_test 8080
  5. *
  6. * @client bin/http_client_test
  7. *
  8. */
  9. #include "requests.h"
  10. #include "axios.h"
  11. using namespace hv;
  12. #include "hthread.h" // import hv_gettid
  13. static void test_http_async_client(HttpClient* cli, int* resp_cnt) {
  14. printf("test_http_async_client request thread tid=%ld\n", hv_gettid());
  15. auto req = std::make_shared<HttpRequest>();
  16. req->method = HTTP_POST;
  17. req->url = "http://127.0.0.1:8080/echo";
  18. req->headers["Connection"] = "keep-alive";
  19. req->body = "This is an async request.";
  20. req->timeout = 10;
  21. cli->sendAsync(req, [resp_cnt](const HttpResponsePtr& resp) {
  22. printf("test_http_async_client response thread tid=%ld\n", hv_gettid());
  23. if (resp == NULL) {
  24. printf("request failed!\n");
  25. } else {
  26. printf("%d %s\r\n", resp->status_code, resp->status_message());
  27. printf("%s\n", resp->body.c_str());
  28. }
  29. *resp_cnt += 1;
  30. });
  31. }
  32. static void test_http_sync_client(HttpClient* cli) {
  33. HttpRequest req;
  34. req.method = HTTP_POST;
  35. req.url = "http://127.0.0.1:8080/echo";
  36. req.headers["Connection"] = "keep-alive";
  37. req.body = "This is a sync request.";
  38. req.timeout = 10;
  39. HttpResponse resp;
  40. int ret = cli->send(&req, &resp);
  41. if (ret != 0) {
  42. printf("request failed!\n");
  43. } else {
  44. printf("%d %s\r\n", resp.status_code, resp.status_message());
  45. printf("%s\n", resp.body.c_str());
  46. }
  47. }
  48. static void test_requests() {
  49. // auto resp = requests::get("http://www.example.com");
  50. //
  51. // make clean && make WITH_OPENSSL=yes
  52. // auto resp = requests::get("https://www.baidu.com");
  53. auto resp = requests::get("http://127.0.0.1:8080/ping");
  54. if (resp == NULL) {
  55. printf("request failed!\n");
  56. } else {
  57. printf("%d %s\r\n", resp->status_code, resp->status_message());
  58. printf("%s\n", resp->body.c_str());
  59. }
  60. // Content-Type: application/json
  61. hv::Json jroot;
  62. jroot["user"] = "admin";
  63. jroot["pswd"] = "123456";
  64. http_headers headers;
  65. headers["Content-Type"] = "application/json";
  66. resp = requests::post("http://127.0.0.1:8080/echo", jroot.dump(), headers);
  67. if (resp == NULL) {
  68. printf("request failed!\n");
  69. } else {
  70. printf("%d %s\r\n", resp->status_code, resp->status_message());
  71. printf("%s\n", resp->body.c_str());
  72. }
  73. // Content-Type: multipart/form-data
  74. std::map<std::string, std::string> params;
  75. params["user"] = "admin";
  76. params["pswd"] = "123456";
  77. resp = requests::uploadFormFile("http://127.0.0.1:8080/echo", "avatar", "avatar.jpg", params);
  78. if (resp == NULL) {
  79. printf("uploadFormFile failed!\n");
  80. } else {
  81. printf("%d %s\r\n", resp->status_code, resp->status_message());
  82. printf("%s\n", resp->body.c_str());
  83. }
  84. /*
  85. size_t filesize = requests::downloadFile("http://www.example.com/index.html", "index.html");
  86. if (filesize == 0) {
  87. printf("downloadFile failed!\n");
  88. } else {
  89. printf("downloadFile success!\n");
  90. }
  91. */
  92. // async
  93. /*
  94. // auto req = std::make_shared<HttpRequest>();
  95. req->url = "http://127.0.0.1:8080/echo";
  96. req->method = HTTP_POST;
  97. req->body = "This is an async request.";
  98. req->timeout = 10;
  99. requests::async(req, [](const HttpResponsePtr& resp) {
  100. if (resp == NULL) {
  101. printf("request failed!\n");
  102. } else {
  103. printf("%d %s\r\n", resp->status_code, resp->status_message());
  104. printf("%s\n", resp->body.c_str());
  105. }
  106. });
  107. */
  108. }
  109. static void test_axios() {
  110. const char* strReq = R"(
  111. {
  112. "method": "POST",
  113. "url": "http://127.0.0.1:8080/echo",
  114. "timeout": 10,
  115. "params": {
  116. "page_no": "1",
  117. "page_size": "10"
  118. },
  119. "headers": {
  120. "Content-Type": "application/json"
  121. },
  122. "body": {
  123. "app_id": "123456",
  124. "app_secret": "abcdefg"
  125. }
  126. }
  127. )";
  128. // sync
  129. auto resp = axios::axios(strReq);
  130. // auto resp = axios::post("http://127.0.0.1:8080/echo", strReq);
  131. if (resp == NULL) {
  132. printf("request failed!\n");
  133. } else {
  134. printf("%d %s\r\n", resp->status_code, resp->status_message());
  135. printf("%s\n", resp->body.c_str());
  136. }
  137. // async
  138. /*
  139. axios::axios(strReq, [](const HttpResponsePtr& resp) {
  140. if (resp == NULL) {
  141. printf("request failed!\n");
  142. } else {
  143. printf("%d %s\r\n", resp->status_code, resp->status_message());
  144. printf("%s\n", resp->body.c_str());
  145. }
  146. });
  147. */
  148. }
  149. int main(int argc, char* argv[]) {
  150. int req_cnt = 0;
  151. if (argc > 1) req_cnt = atoi(argv[1]);
  152. if (req_cnt == 0) req_cnt = 1;
  153. HttpClient sync_client;
  154. HttpClient async_client;
  155. int resp_cnt = 0;
  156. for (int i = 0; i < req_cnt; ++i) {
  157. test_http_async_client(&async_client, &resp_cnt);
  158. test_http_sync_client(&sync_client);
  159. test_requests();
  160. test_axios();
  161. }
  162. // demo wait async finished
  163. while (resp_cnt < req_cnt) hv_delay(100);
  164. printf("finished!\n");
  165. return 0;
  166. }