diff --git a/README.md b/README.md index 15f3f03..76b0f2d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,59 @@ -# cppurl -C++ libcurl framework \ No newline at end of file +# Rest client library, using libcurl + +A small framework for simple client-side REST API requests for C++. It uses libcurl. It got its name from a play on the words curl and cpp. + +## Features + +- Object oriented +- Adjustable headers +- Configurable user agent +- Adjustable timeout +- Native C++ containers: map, string +- QA object +- Curl and HTTP code status +- It is possible to turn off certificate validation +- A configurable version of the HTTP protocol + +## Installation + +Just download the latest release and unzip it into your project. You can turn it on with: + +``` +#include "cppurl/lib/cppurl.hpp" +using namespace marcelb; +``` + +## Usage + +```c++ +// init, and sets +Curl rest; +string header_value = "3849f438uf9uedu8ejweoijwejd09230"; +rest.header("API", header_value).timeout(700) + .httpv(HTTP2).sslverifyoff(); +// execute and print +cout << rest.get("https://reqres.in/api/users/2") << endl << + "Curl status " << rest.curlStatus << endl << + "HTTP status " << rest.httpStatus << endl; + +for (auto header : rest.responseHeader) { + cout << header.first << " " << header.second << endl; +} + +``` +## License + +[APACHE 2.0](http://www.apache.org/licenses/LICENSE-2.0/) + + +## Support & Feedback + +For support and any feedback, contact the address: marcelb96@yahoo.com. + +## Contributing + +Contributions are always welcome! + +Feel free to fork and start working with or without a later pull request. Or contact for suggest and request an option. + diff --git a/lib/cppurl.hpp b/lib/cppurl.hpp index 5c79323..f8dca85 100644 --- a/lib/cppurl.hpp +++ b/lib/cppurl.hpp @@ -11,12 +11,27 @@ namespace marcelb { using namespace std; +/** + * Callback function for parsing the HTTP body +*/ static size_t bodyCallback(void *contents, size_t size, size_t nmemb, void *body_ptr); + +/** + * Callback function for parsing HTTP headers +*/ static size_t headerCallback(char* buffer, size_t size, size_t nitems, void* header_ptr); + +/** + * HTTP supported protocols +*/ enum http_version { DEFAULT, HTTP1_0, HTTP1_1, HTTP2, HTTP2TLS, HTTP2PK, HTTP3 = 30}; +/** + * Class for curl request and response +*/ class Curl { // input + CURL *curl; CURLcode res; struct curl_slist *headers = NULL; @@ -28,60 +43,62 @@ class Curl { public: // output + + // Curl code response status CURLcode curlStatus; + // HTTP code response status long httpStatus; + // HTTP response headers map responseHeader; + // HTTP body string body; /** - * Postavi zaglavlje s ključem i vrijednošću - * Novi pozivi ne brišu stara zaglavlja, ponovljena se prepišu + * Set header with key and value + * New calls do not delete old headers, repeated ones are overwritten */ Curl& header(const string& key, const string& value); /** - * Postavi zaglavlja iz mape - * Ponovan poziv prepisat će ona zaglavlja koja postoje + * Set headers from folder + * The redial will overwrite those headers that exist */ Curl& header(const map &_headers); /** - * Postavi u zaglavlje User-Agent + * Set in User-Agent header */ Curl& useragent(const string& useragent_); /** - * Postavi vrijeme isteka zahtjeva + * Set request timeout */ - Curl& timeout(const long _timeout); /** - * Omogući/onemogući validaciju certifikata kod SSL veza + * Disable certificate validation for SSL connections */ - Curl& sslverifyoff(); /** - * Postavi verziju HTTP protokola - * HTTP1_0 - HTTP1_1 - HTTP2 - HTTP2TLS - HTTP2PK - HTTP3 + * Set HTTP protocol version + * HTTP1_0 - HTTP1_1 - HTTP2 - HTTP2TLS - HTTP2PK - HTTP3 */ - Curl& httpv(const http_version protocol_v); /** - * Izvršiv HTTP GET zahtjev - * Vraća string HTTP tjela + * Executable HTTP GET request + * Returns the HTTP body as string */ string get(const string& req); /** - * Obriši spremljeno zaglavlje + * Clear saved headers */ Curl& clearheader(); /** - * Obrši trenutnog User-Agent -a + * Clear the current User-Agent */ Curl& clearuseragent(); diff --git a/test/test b/test/test index 1dc0ef3..5231ea9 100755 Binary files a/test/test and b/test/test differ diff --git a/test/test.cpp b/test/test.cpp index c5e654c..f44ca7f 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -12,11 +12,12 @@ using namespace marcelb; int main () { Curl rest; - string header_value = "jebiga"; - rest.header("API", header_value); + string header_value = "3849f438uf9uedu8ejweoijwejd09230"; + rest.header("API", header_value).timeout(700) + .httpv(HTTP2).sslverifyoff(); cout << rest.get("https://reqres.in/api/users/2") << endl << "Curl status " << rest.curlStatus << endl << - " http status " << rest.httpStatus << endl; + "HTTP status " << rest.httpStatus << endl; for (auto header : rest.responseHeader) { cout << header.first << " " << header.second << endl;