|
1 | 1 | # curl-cpp-wrapper
|
2 |
| -Simple libcurl wrapper in C++ |
| 2 | + |
| 3 | +A very simple HTTP client (C++ wrapper for curl). Requires libcurl http://curl.haxx.se |
| 4 | + |
| 5 | +If you are using Visual C++ your also have to install msinttypes (headers are provided in "include" directory). |
| 6 | + |
| 7 | +Usage |
| 8 | + |
| 9 | +Do a GET request: |
| 10 | +```cpp |
| 11 | +NetworkClient nc; |
| 12 | +nc.doGet("http://google.com/?q=" + nc.urlEncode("Smelly cat"); |
| 13 | +std::cout << nc.responseBody(); |
| 14 | +``` |
| 15 | + |
| 16 | +Do a POST request: |
| 17 | +```cpp |
| 18 | +nc.setUrl("https://www.googleapis.com/oauth2/v3/token"); |
| 19 | +nc.addQueryParam("refresh_token", refreshToken); |
| 20 | +nc.addQueryParam("client_id", clientId); |
| 21 | +nc.addQueryParam("client_secret", clientSecret); |
| 22 | +nc.addQueryParam("grant_type", "refresh_token"); |
| 23 | +nc.doPost(""); |
| 24 | +``` |
| 25 | + |
| 26 | +Do a POST request with RAW data: |
| 27 | +```cpp |
| 28 | +nc.setUrl("https://www.googleapis.com/oauth2/v3/token"); |
| 29 | +nc.doPost("param1=value¶m=value"); |
| 30 | +``` |
| 31 | + |
| 32 | +Downloading a file: |
| 33 | +```cpp |
| 34 | +NetworkClient nc; |
| 35 | +nc.setOutputFile("d:\\image.png"); //only UTF-8 file names are supported on Windows |
| 36 | +nc.doGet("http://i.imgur.com/DDf2wbJ.png"); |
| 37 | +``` |
| 38 | + |
| 39 | +Uploading a file: |
| 40 | +```cpp |
| 41 | +#include "NetworkClient.h" |
| 42 | +#include "Core/Utils/CoreUtils.h" |
| 43 | +NetworkClient nc; |
| 44 | +std::string fileName = "c:\\test\\file.txt"; //only UTF-8 file names are supported on Windows |
| 45 | +//std::string fileName = IuCoreUtils::SystemLocaleToUtf8(fileName); <-- if you want to upload a file with ANSI filename on Windows |
| 46 | +nc.setUrl("http://takebin.com/action"); |
| 47 | +nc.addQueryParamFile("file", fileName, IuCoreUtils::ExtractFileName(FileName),""); |
| 48 | +nc.addQueryParam("fileDesc", "cool file"); |
| 49 | + |
| 50 | +nc.doUploadMultipartData(); |
| 51 | +if ( nc.responseCode() == 200 ) { |
| 52 | + std::cout << nc.responseBody(); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Do a PUT request: |
| 57 | +```cpp |
| 58 | +nc.setMethod("PUT"); |
| 59 | +nc.setUrl("https://www.googleapis.com/drive/v2/files/" + id); |
| 60 | +nc.addQueryHeader("Authorization", "Basic "); |
| 61 | +nc.addQueryHeader("Content-Type", "application/json"); |
| 62 | +std::string postData = "{\"title\" = \"SmellyCat.jpg\"}"; |
| 63 | +nc.doUpload("", postData); |
| 64 | +``` |
| 65 | + |
| 66 | +Uploading a file to FTP: |
| 67 | +```cpp |
| 68 | +std::string fileName = "c:\\test\\file.txt"; |
| 69 | +nc.setUrl("ftp://example.com"); |
| 70 | +nc.setMethod("PUT"); |
| 71 | +nc.doUpload(fileName, ""); |
| 72 | +``` |
| 73 | + |
| 74 | +Using proxy: |
| 75 | +```cpp |
| 76 | +nc.setProxy("127.0.0.1", "8888", CURLPROXY_HTTP); // CURLPROXY_HTTP, CURLPROXY_SOCKS4, CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5, CURLPROXY_SOCKS5_HOSTNAME |
| 77 | +``` |
| 78 | +Custom request header: |
| 79 | +``` |
| 80 | +nc.addQueryHeader("User-Agent", "Mozilla/5.0"); |
| 81 | +``` |
| 82 | + |
| 83 | +If you want to use HTTPS, you should compile libcurl with openssl support (you could also use WinSSL on Windows). |
| 84 | +If you compile libcurl for windows with OpenSSL support (instead of WinSSL), you should put "curl-ca-bundle.crt" file into your application's directory). |
0 commit comments