http_client_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include "hthread.h" // import hv_gettid
  12. static void test_http_async_client(http_client_t* cli, int* finished) {
  13. printf("test_http_async_client request thread tid=%ld\n", hv_gettid());
  14. HttpRequestPtr req(new HttpRequest);
  15. req->method = HTTP_POST;
  16. req->url = "127.0.0.1:8080/echo";
  17. req->headers["Connection"] = "keep-alive";
  18. req->body = "This is an async request.";
  19. req->timeout = 10;
  20. http_client_send_async(cli, req, [finished](const HttpResponsePtr& resp) {
  21. printf("test_http_async_client response thread tid=%ld\n", hv_gettid());
  22. if (resp == NULL) {
  23. printf("request failed!\n");
  24. } else {
  25. printf("%d %s\r\n", resp->status_code, resp->status_message());
  26. printf("%s\n", resp->body.c_str());
  27. }
  28. *finished = 1;
  29. });
  30. }
  31. static void test_http_sync_client(http_client_t* cli) {
  32. HttpRequest req;
  33. req.method = HTTP_POST;
  34. req.url = "127.0.0.1:8080/echo";
  35. req.headers["Connection"] = "keep-alive";
  36. req.body = "This is a sync request.";
  37. req.timeout = 10;
  38. HttpResponse resp;
  39. int ret = http_client_send(cli, &req, &resp);
  40. if (ret != 0) {
  41. printf("request failed!\n");
  42. } else {
  43. printf("%d %s\r\n", resp.status_code, resp.status_message());
  44. printf("%s\n", resp.body.c_str());
  45. }
  46. }
  47. static void test_requests() {
  48. // auto resp = requests::get("http://www.example.com");
  49. //
  50. // make clean && make WITH_OPENSSL=yes
  51. // auto resp = requests::get("https://www.baidu.com");
  52. auto resp = requests::get("http://127.0.0.1:8080/ping");
  53. if (resp == NULL) {
  54. printf("request failed!\n");
  55. } else {
  56. printf("%d %s\r\n", resp->status_code, resp->status_message());
  57. printf("%s\n", resp->body.c_str());
  58. }
  59. hv::Json jroot;
  60. jroot["user"] = "admin";
  61. jroot["pswd"] = "123456";
  62. http_headers headers;
  63. headers["Content-Type"] = "application/json";
  64. resp = requests::post("127.0.0.1:8080/echo", jroot.dump(), headers);
  65. if (resp == NULL) {
  66. printf("request failed!\n");
  67. } else {
  68. printf("%d %s\r\n", resp->status_code, resp->status_message());
  69. printf("%s\n", resp->body.c_str());
  70. }
  71. // async
  72. /*
  73. int finished = 0;
  74. Request req(new HttpRequest);
  75. req->url = "http://127.0.0.1:8080/echo";
  76. req->method = HTTP_POST;
  77. req->body = "This is an async request.";
  78. req->timeout = 10;
  79. requests::async(req, [&finished](const HttpResponsePtr& resp) {
  80. if (resp == NULL) {
  81. printf("request failed!\n");
  82. } else {
  83. printf("%d %s\r\n", resp->status_code, resp->status_message());
  84. printf("%s\n", resp->body.c_str());
  85. }
  86. finished = 1;
  87. });
  88. */
  89. }
  90. static void test_axios() {
  91. const char* strReq = R"(
  92. {
  93. "method": "POST",
  94. "url": "http://127.0.0.1:8080/echo",
  95. "timeout": 10,
  96. "params": {
  97. "page_no": "1",
  98. "page_size": "10"
  99. },
  100. "headers": {
  101. "Content-Type": "application/json"
  102. },
  103. "body": {
  104. "app_id": "123456",
  105. "app_secret": "abcdefg"
  106. }
  107. }
  108. )";
  109. // sync
  110. auto resp = axios::axios(strReq);
  111. // auto resp = axios::post("http://127.0.0.1:8080/echo", strReq);
  112. if (resp == NULL) {
  113. printf("request failed!\n");
  114. } else {
  115. printf("%d %s\r\n", resp->status_code, resp->status_message());
  116. printf("%s\n", resp->body.c_str());
  117. }
  118. // async
  119. /*
  120. int finished = 0;
  121. axios::axios(strReq, [&finished](const HttpResponsePtr& resp) {
  122. if (resp == NULL) {
  123. printf("request failed!\n");
  124. } else {
  125. printf("%d %s\r\n", resp->status_code, resp->status_message());
  126. printf("%s\n", resp->body.c_str());
  127. }
  128. finished = 1;
  129. });
  130. */
  131. }
  132. int main(int argc, char* argv[]) {
  133. int cnt = 0;
  134. if (argc > 1) cnt = atoi(argv[1]);
  135. if (cnt == 0) cnt = 1;
  136. http_client_t* sync_client = http_client_new();
  137. http_client_t* async_client = http_client_new();
  138. int finished = 0;
  139. for (int i = 0; i < cnt; ++i) {
  140. test_http_async_client(async_client, &finished);
  141. test_http_sync_client(sync_client);
  142. test_requests();
  143. test_axios();
  144. }
  145. http_client_del(sync_client);
  146. // demo wait async finished
  147. while (!finished) hv_delay(100);
  148. printf("finished!\n");
  149. http_client_del(async_client);
  150. return 0;
  151. }