Coder Social home page Coder Social logo

curl-cpp-wrapper's Introduction

curl-cpp-wrapper

A very simple HTTP client (C++ wrapper for curl). Requires libcurl http://curl.haxx.se

If you are using Visual C++ your also have to install msinttypes (headers are provided in "include" directory).

Usage

Do a GET request:

NetworkClient nc;
nc.doGet("http://google.com/?q=" + nc.urlEncode("Smelly cat"));
std::cout << nc.responseBody();

Do a POST request:

nc.setUrl("https://www.googleapis.com/oauth2/v3/token");
nc.addQueryParam("refresh_token", refreshToken); 
nc.addQueryParam("client_id", clientId); 
nc.addQueryParam("client_secret", clientSecret); 
nc.addQueryParam("grant_type", "refresh_token"); 
nc.doPost();
std::cout << nc.responseBody();

Do a POST request with RAW data:

nc.setUrl("https://www.googleapis.com/oauth2/v3/token");
nc.doPost("param1=value&param=value");

Downloading a file:

NetworkClient nc;
nc.setOutputFile("d:\\image.png"); //only UTF-8 file names are supported on Windows
nc.doGet("http://i.imgur.com/DDf2wbJ.png");

Uploading a file:

#include "NetworkClient.h"
#include "Core/Utils/CoreUtils.h"
NetworkClient nc;
std::string fileName = "c:\\test\\file.txt"; //only UTF-8 file names are supported on Windows
//std::string fileName = IuCoreUtils::SystemLocaleToUtf8(fileName); <-- if you want to upload a file with ANSI filename on Windows
nc.setUrl("http://takebin.com/action");
nc.addQueryParamFile("file", fileName, IuCoreUtils::ExtractFileName(FileName),"");
nc.addQueryParam("fileDesc", "cool file");

nc.doUploadMultipartData();
if ( nc.responseCode() == 200 )  {
		std::cout << nc.responseBody();
}

Do a PUT request:

nc.setMethod("PUT");
nc.setUrl("https://www.googleapis.com/drive/v2/files/" + id);
nc.addQueryHeader("Authorization", "Basic ");
nc.addQueryHeader("Content-Type", "application/json");
std::string postData = "{\"title\" = \"SmellyCat.jpg\"}";
nc.doUpload("", postData);

Uploading a file to FTP:

std::string fileName = "c:\\test\\file.txt";
nc.setUrl("ftp://example.com");
nc.setMethod("PUT");
nc.doUpload(fileName, "");

Using proxy:

nc.setProxy("127.0.0.1", "8888", CURLPROXY_HTTP); // CURLPROXY_HTTP, CURLPROXY_SOCKS4, CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5, CURLPROXY_SOCKS5_HOSTNAME

Custom request header:

nc.addQueryHeader("User-Agent", "Mozilla/5.0");

If you want to use HTTPS, you should compile libcurl with openssl support (you could also use WinSSL on Windows). 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).

curl-cpp-wrapper's People

Contributors

zenden2k avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.