Comments, README

dev v0.6
marcelb 8 months ago
parent dc26ed563f
commit beaddd239b
  1. 60
      README.md
  2. 47
      lib/cppurl.hpp
  3. BIN
      test/test
  4. 7
      test/test.cpp

@ -1,3 +1,59 @@
# cppurl
C++ libcurl framework # 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.

@ -11,12 +11,27 @@ namespace marcelb {
using namespace std; 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); 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); 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}; enum http_version { DEFAULT, HTTP1_0, HTTP1_1, HTTP2, HTTP2TLS, HTTP2PK, HTTP3 = 30};
/**
* Class for curl request and response
*/
class Curl { class Curl {
// input // input
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
@ -28,60 +43,62 @@ class Curl {
public: public:
// output // output
// Curl code response status
CURLcode curlStatus; CURLcode curlStatus;
// HTTP code response status
long httpStatus; long httpStatus;
// HTTP response headers
map<string, string> responseHeader; map<string, string> responseHeader;
// HTTP body
string body; string body;
/** /**
* Postavi zaglavlje s ključem i vrijednošću * Set header with key and value
* Novi pozivi ne brišu stara zaglavlja, ponovljena se prepišu * New calls do not delete old headers, repeated ones are overwritten
*/ */
Curl& header(const string& key, const string& value); Curl& header(const string& key, const string& value);
/** /**
* Postavi zaglavlja iz mape * Set headers from folder
* Ponovan poziv prepisat će ona zaglavlja koja postoje * The redial will overwrite those headers that exist
*/ */
Curl& header(const map<string, string> &_headers); Curl& header(const map<string, string> &_headers);
/** /**
* Postavi u zaglavlje User-Agent * Set in User-Agent header
*/ */
Curl& useragent(const string& useragent_); Curl& useragent(const string& useragent_);
/** /**
* Postavi vrijeme isteka zahtjeva * Set request timeout
*/ */
Curl& timeout(const long _timeout); Curl& timeout(const long _timeout);
/** /**
* Omogući/onemogući validaciju certifikata kod SSL veza * Disable certificate validation for SSL connections
*/ */
Curl& sslverifyoff(); Curl& sslverifyoff();
/** /**
* Postavi verziju HTTP protokola * Set HTTP protocol version
* HTTP1_0 - HTTP1_1 - HTTP2 - HTTP2TLS - HTTP2PK - HTTP3 * HTTP1_0 - HTTP1_1 - HTTP2 - HTTP2TLS - HTTP2PK - HTTP3
*/ */
Curl& httpv(const http_version protocol_v); Curl& httpv(const http_version protocol_v);
/** /**
* Izvršiv HTTP GET zahtjev * Executable HTTP GET request
* Vraća string HTTP tjela * Returns the HTTP body as string
*/ */
string get(const string& req); string get(const string& req);
/** /**
* Obriši spremljeno zaglavlje * Clear saved headers
*/ */
Curl& clearheader(); Curl& clearheader();
/** /**
* Obrši trenutnog User-Agent -a * Clear the current User-Agent
*/ */
Curl& clearuseragent(); Curl& clearuseragent();

Binary file not shown.

@ -12,11 +12,12 @@ using namespace marcelb;
int main () { int main () {
Curl rest; Curl rest;
string header_value = "jebiga"; string header_value = "3849f438uf9uedu8ejweoijwejd09230";
rest.header("API", header_value); rest.header("API", header_value).timeout(700)
.httpv(HTTP2).sslverifyoff();
cout << rest.get("https://reqres.in/api/users/2") << endl << cout << rest.get("https://reqres.in/api/users/2") << endl <<
"Curl status " << rest.curlStatus << endl << "Curl status " << rest.curlStatus << endl <<
" http status " << rest.httpStatus << endl; "HTTP status " << rest.httpStatus << endl;
for (auto header : rest.responseHeader) { for (auto header : rest.responseHeader) {
cout << header.first << " " << header.second << endl; cout << header.first << " " << header.second << endl;

Loading…
Cancel
Save