1
0

http_client_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. HttpRequestPtr req(new 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. requests::Request req(new HttpRequest);
  75. req->method = HTTP_POST;
  76. req->url = "http://127.0.0.1:8080/echo";
  77. req->content_type = MULTIPART_FORM_DATA;
  78. req->SetFormData("username", "admin");
  79. req->SetFormFile("avatar", "avatar.jpg");
  80. resp = requests::request(req);
  81. if (resp == NULL) {
  82. printf("request failed!\n");
  83. } else {
  84. printf("%d %s\r\n", resp->status_code, resp->status_message());
  85. printf("%s\n", resp->body.c_str());
  86. }
  87. // async
  88. /*
  89. // Request req(new HttpRequest);
  90. req->url = "http://127.0.0.1:8080/echo";
  91. req->method = HTTP_POST;
  92. req->body = "This is an async request.";
  93. req->timeout = 10;
  94. requests::async(req, [](const HttpResponsePtr& resp) {
  95. if (resp == NULL) {
  96. printf("request failed!\n");
  97. } else {
  98. printf("%d %s\r\n", resp->status_code, resp->status_message());
  99. printf("%s\n", resp->body.c_str());
  100. }
  101. });
  102. */
  103. }
  104. static void test_axios() {
  105. const char* strReq = R"(
  106. {
  107. "method": "POST",
  108. "url": "http://127.0.0.1:8080/echo",
  109. "timeout": 10,
  110. "params": {
  111. "page_no": "1",
  112. "page_size": "10"
  113. },
  114. "headers": {
  115. "Content-Type": "application/json"
  116. },
  117. "body": {
  118. "app_id": "123456",
  119. "app_secret": "abcdefg"
  120. }
  121. }
  122. )";
  123. // sync
  124. auto resp = axios::axios(strReq);
  125. // auto resp = axios::post("http://127.0.0.1:8080/echo", strReq);
  126. if (resp == NULL) {
  127. printf("request failed!\n");
  128. } else {
  129. printf("%d %s\r\n", resp->status_code, resp->status_message());
  130. printf("%s\n", resp->body.c_str());
  131. }
  132. // async
  133. /*
  134. axios::axios(strReq, [](const HttpResponsePtr& resp) {
  135. if (resp == NULL) {
  136. printf("request failed!\n");
  137. } else {
  138. printf("%d %s\r\n", resp->status_code, resp->status_message());
  139. printf("%s\n", resp->body.c_str());
  140. }
  141. });
  142. */
  143. }
  144. int main(int argc, char* argv[]) {
  145. int req_cnt = 0;
  146. if (argc > 1) req_cnt = atoi(argv[1]);
  147. if (req_cnt == 0) req_cnt = 1;
  148. HttpClient sync_client;
  149. HttpClient async_client;
  150. int resp_cnt = 0;
  151. for (int i = 0; i < req_cnt; ++i) {
  152. test_http_async_client(&async_client, &resp_cnt);
  153. test_http_sync_client(&sync_client);
  154. test_requests();
  155. test_axios();
  156. }
  157. // demo wait async finished
  158. while (resp_cnt < req_cnt) hv_delay(100);
  159. printf("finished!\n");
  160. return 0;
  161. }