├── .gitignore ├── README.md ├── Request.cpp ├── Request.h ├── nlohmann └── json.hpp └── test └── test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | a.out 2 | .vscode 3 | *.h.gch 4 | .DS_store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ Http Request Library 2 | 3 | ## Descriptiom 4 | 5 | This is a c++ library for web Http API request, you can use it like Python's request. 6 | 7 | ## Installation 8 | 9 | Use g++ for compile 10 | 11 | ```bash 12 | g++ -std=c++11 -lcurl [your_program_files] 13 | ``` 14 | 15 | You can run the following command for testing and example 16 | 17 | ```bash 18 | g++ -std=c++11 -lcurl test/test.cpp Request.cpp Request.h 19 | ``` 20 | 21 | And run command below to test 22 | 23 | ```bash 24 | ./a.out 25 | ``` 26 | 27 | ## Usage 28 | 29 | The `Request` class provides simple methods to make HTTP GET and POST requests. Here's a guide on how to use them: 30 | 31 | ### Initialization 32 | 33 | First, you need to create an instance of the `Request` class. 34 | 35 | ```cpp 36 | Request request; 37 | ``` 38 | 39 | ### Making a GET Request 40 | 41 | To make a GET request, use the `Get` method and provide the URL. 42 | 43 | ```cpp 44 | std::string response = request.Get("https://example.com"); 45 | ``` 46 | 47 | ### Making a POST Request 48 | 49 | For POST requests, use the `Post` method. Provide the URL and a JSON object with the data. 50 | 51 | ```cpp 52 | nlohmann::json jsonData; 53 | jsonData["key"] = "value"; 54 | std::string response = request.Post("https://example.com/post", jsonData); 55 | ``` 56 | 57 | ### Accessing Response Data 58 | 59 | Once you've made a request, you can access the response data, HTTP status, headers, cookies, and history as follows: 60 | 61 | - Response content: 62 | 63 | ```cpp 64 | std::string content = request.get_content(); 65 | ``` 66 | 67 | - HTTP status: 68 | 69 | ```cpp 70 | int status = request.get_status(); 71 | ``` 72 | 73 | - Response headers: 74 | 75 | ```cpp 76 | std::string headers = request.get_response_header(); 77 | ``` 78 | 79 | - Cookies: 80 | 81 | ```cpp 82 | std::string cookies = request.get_response_cookie(); 83 | ``` 84 | 85 | - History (Note: this feature may not be fully implemented yet): 86 | 87 | ```cpp 88 | std::string history = request.get_response_history(); 89 | ``` 90 | 91 | ### SetHeaders 92 | 93 | If you need to add HTTP headers to a request, use nlohmann json structure 94 | 95 | ```cpp 96 | nlohmann::json headers; 97 | headers["User-Agent"] = "MyApp"; 98 | headers["Authorization"] = "Bearer 12345"; 99 | 100 | request.SetHeaders(headers); 101 | ``` 102 | 103 | If you need to get HTTP headers for a request, call the GetHeaders function, and it will return in nlohmann json structure 104 | 105 | ```cpp 106 | nlohmann::json headers; 107 | headers = request.GetHeaders(); 108 | ``` 109 | -------------------------------------------------------------------------------- /Request.cpp: -------------------------------------------------------------------------------- 1 | #include "Request.h" 2 | Request::Request() 3 | { 4 | content = ""; 5 | curl = curl_easy_init(); 6 | http_status = -1; 7 | response_header = ""; 8 | response_cookie = ""; 9 | response_history = ""; 10 | curl_headers = NULL; 11 | } 12 | 13 | Request::~Request() 14 | { 15 | if (curl_headers) 16 | { 17 | curl_slist_free_all(curl_headers); 18 | } 19 | } 20 | 21 | size_t Request::write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) 22 | { 23 | std::string *str = (std::string *)userdata; 24 | str->append(ptr, size * nmemb); 25 | return size * nmemb; 26 | } 27 | 28 | std::string Request::Get(const std::string &URL) 29 | { 30 | if (!curl) 31 | { 32 | std::cerr << "Error initializing libcurl" << std::endl; 33 | return ""; 34 | } 35 | 36 | curl_easy_setopt(curl, CURLOPT_URL, URL.c_str()); 37 | 38 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); 39 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content); 40 | 41 | curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response_header); 42 | curl_easy_setopt(curl, CURLOPT_COOKIELIST, &response_cookie); 43 | 44 | CURLcode result = curl_easy_perform(curl); 45 | 46 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status); 47 | curl_easy_cleanup(curl); 48 | 49 | if (result != CURLE_OK) 50 | { 51 | std::cerr << "Error performing HTTP request: " << curl_easy_strerror(result) << std::endl; 52 | return ""; 53 | } 54 | return content; 55 | } 56 | 57 | std::string Request::Post(const std::string &URL, const nlohmann::json &jsonData) 58 | { 59 | if (!curl) 60 | { 61 | std::cerr << "Error initializing libcurl" << std::endl; 62 | return ""; 63 | } 64 | 65 | std::string postData = jsonData.dump(); 66 | 67 | struct curl_slist *headers = NULL; 68 | headers = curl_slist_append(headers, "Content-Type: application/json"); 69 | 70 | curl_easy_setopt(curl, CURLOPT_URL, URL.c_str()); 71 | curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str()); 72 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 73 | 74 | curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response_header); 75 | curl_easy_setopt(curl, CURLOPT_COOKIELIST, &response_cookie); 76 | 77 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); 78 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content); 79 | 80 | curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response_header); 81 | curl_easy_setopt(curl, CURLOPT_COOKIELIST, &response_cookie); 82 | 83 | CURLcode result = curl_easy_perform(curl); 84 | 85 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_status); 86 | curl_easy_cleanup(curl); 87 | curl_slist_free_all(headers); 88 | 89 | if (result != CURLE_OK) 90 | { 91 | std::cerr << "Error performing HTTP request: " << curl_easy_strerror(result) << std::endl; 92 | return ""; 93 | } 94 | return content; 95 | } 96 | 97 | HTTPSTATUS Request::get_status() 98 | { 99 | return http_status; 100 | } 101 | 102 | std::string Request::get_content() 103 | { 104 | return content; 105 | } 106 | 107 | void Request::SetHeaders(const nlohmann::json &headers) 108 | { 109 | 110 | if (curl_headers) 111 | { 112 | curl_slist_free_all(curl_headers); 113 | curl_headers = NULL; 114 | } 115 | 116 | for (auto &[name, value] : headers.items()) 117 | { 118 | std::string header = name + ": " + value.get(); 119 | curl_headers = curl_slist_append(curl_headers, header.c_str()); 120 | } 121 | 122 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers); 123 | } 124 | 125 | nlohmann::json Request::GetHeaders() 126 | { 127 | nlohmann::json headers; 128 | struct curl_slist *header_list; 129 | curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &header_list); 130 | for (struct curl_slist *next = header_list; next; next = next->next) 131 | { 132 | std::string header = next->data; 133 | std::string name = header.substr(0, header.find_first_of('=')); 134 | std::string value = header.substr(header.find_first_of('=') + 1); 135 | headers[name] = value; 136 | } 137 | return headers; 138 | } -------------------------------------------------------------------------------- /Request.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "nlohmann/json.hpp" 4 | 5 | #define HTTPSTATUS int 6 | class Request 7 | { 8 | private: 9 | std::string content; 10 | std::string response_header; 11 | std::string response_cookie; 12 | std::string response_history; 13 | CURL *curl; 14 | HTTPSTATUS http_status; 15 | struct curl_slist *curl_headers; 16 | 17 | public: 18 | Request(); 19 | ~Request(); 20 | 21 | static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata); 22 | std::string Get(const std::string &URL); 23 | std::string Post(const std::string &URL, const nlohmann::json &jsonData); 24 | 25 | HTTPSTATUS get_status(); 26 | std::string get_content(); 27 | void SetHeaders(const nlohmann::json &headers); 28 | nlohmann::json GetHeaders(); 29 | std::string get_response_header() const { return response_header; } 30 | std::string get_response_cookie() const { return response_cookie; } 31 | std::string get_response_history() const { return response_history; } 32 | }; 33 | -------------------------------------------------------------------------------- /test/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../Request.h" 3 | #include "../nlohmann/json.hpp" 4 | using namespace std; 5 | int test_get() 6 | { 7 | cout << "----------Start GET test----------\n"; 8 | try 9 | { 10 | Request re; 11 | string s; 12 | s = re.Get("https://example.com"); 13 | cout << re.get_content() << endl; 14 | cout << "HTTP Status: " << re.get_status() << endl; 15 | cout << "Response Header: " << re.get_response_header() << endl; 16 | cout << "Response Cookie: " << re.get_response_cookie() << endl; 17 | cout << "Response History: " << re.get_response_history() << endl; 18 | } 19 | catch (...) 20 | { 21 | cout << "----------End GET test----------\n"; 22 | return -1; 23 | } 24 | cout << "----------End GET test----------\n\n"; 25 | return 0; 26 | } 27 | 28 | int test_post() 29 | { 30 | cout << "----------Start Post test----------\n\n"; 31 | try 32 | { 33 | string testURL = "https://jsonplaceholder.typicode.com/posts"; 34 | Request request; 35 | 36 | nlohmann::json jsonData; 37 | jsonData["title"] = "Test Title"; 38 | jsonData["body"] = "Test Body"; 39 | jsonData["userId"] = 1; 40 | 41 | string response = request.Post(testURL, jsonData); 42 | int statusCode = request.get_status(); 43 | 44 | if (!response.empty()) 45 | { 46 | std::cout << "HTTP Status: " << statusCode << std::endl; 47 | std::cout << "Response: " << response << std::endl; 48 | cout << "Response Header: " << request.get_response_header() << endl; 49 | cout << "Response Cookie: " << request.get_response_cookie() << endl; 50 | cout << "Response History: " << request.get_response_history() << endl; 51 | } 52 | else 53 | { 54 | std::cerr << "Failed to send POST request" << std::endl; 55 | } 56 | } 57 | catch (...) 58 | { 59 | cout << "----------End POST test----------\n\n"; 60 | return -1; 61 | } 62 | cout << "----------End POST test----------\n\n"; 63 | return 0; 64 | } 65 | 66 | int main() 67 | { 68 | int get_status = test_get(); 69 | int post_status = test_post(); 70 | string get_str = (get_status == 0) ? "Pass" : "Fail!"; 71 | string post_str = (post_status == 0) ? "Pass" : "Fail!"; 72 | cout << "----------Check List----------\n"; 73 | cout << "Get Status: " << get_str << endl; 74 | cout << "Post Status: " << post_str << endl; 75 | cout << "----------End Check List----------\n"; 76 | return 0; 77 | } 78 | --------------------------------------------------------------------------------