1
0

http_client_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Content-Type: application/json
  60. hv::Json jroot;
  61. jroot["user"] = "admin";
  62. jroot["pswd"] = "123456";
  63. http_headers headers;
  64. headers["Content-Type"] = "application/json";
  65. resp = requests::post("127.0.0.1:8080/echo", jroot.dump(), headers);
  66. if (resp == NULL) {
  67. printf("request failed!\n");
  68. } else {
  69. printf("%d %s\r\n", resp->status_code, resp->status_message());
  70. printf("%s\n", resp->body.c_str());
  71. }
  72. // Content-Type: multipart/form-data
  73. requests::Request req(new HttpRequest);
  74. req->method = HTTP_POST;
  75. req->url = "http://127.0.0.1:8080/echo";
  76. req->content_type = MULTIPART_FORM_DATA;
  77. req->SetFormData("username", "admin");
  78. req->SetFormFile("avatar", "avatar.jpg");
  79. resp = requests::request(req);
  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. // async
  87. /*
  88. int finished = 0;
  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, [&finished](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. finished = 1;
  102. });
  103. */
  104. }
  105. static void test_axios() {
  106. const char* strReq = R"(
  107. {
  108. "method": "POST",
  109. "url": "http://127.0.0.1:8080/echo",
  110. "timeout": 10,
  111. "params": {
  112. "page_no": "1",
  113. "page_size": "10"
  114. },
  115. "headers": {
  116. "Content-Type": "application/json"
  117. },
  118. "body": {
  119. "app_id": "123456",
  120. "app_secret": "abcdefg"
  121. }
  122. }
  123. )";
  124. // sync
  125. auto resp = axios::axios(strReq);
  126. // auto resp = axios::post("http://127.0.0.1:8080/echo", strReq);
  127. if (resp == NULL) {
  128. printf("request failed!\n");
  129. } else {
  130. printf("%d %s\r\n", resp->status_code, resp->status_message());
  131. printf("%s\n", resp->body.c_str());
  132. }
  133. // async
  134. /*
  135. int finished = 0;
  136. axios::axios(strReq, [&finished](const HttpResponsePtr& resp) {
  137. if (resp == NULL) {
  138. printf("request failed!\n");
  139. } else {
  140. printf("%d %s\r\n", resp->status_code, resp->status_message());
  141. printf("%s\n", resp->body.c_str());
  142. }
  143. finished = 1;
  144. });
  145. */
  146. }
  147. int main(int argc, char* argv[]) {
  148. int cnt = 0;
  149. if (argc > 1) cnt = atoi(argv[1]);
  150. if (cnt == 0) cnt = 1;
  151. http_client_t* sync_client = http_client_new();
  152. http_client_t* async_client = http_client_new();
  153. int finished = 0;
  154. for (int i = 0; i < cnt; ++i) {
  155. test_http_async_client(async_client, &finished);
  156. test_http_sync_client(sync_client);
  157. test_requests();
  158. test_axios();
  159. }
  160. http_client_del(sync_client);
  161. // demo wait async finished
  162. while (!finished) hv_delay(100);
  163. printf("finished!\n");
  164. http_client_del(async_client);
  165. return 0;
  166. }