Giri's C++ Support Library
C++ library providing everything you need to quickly create awesome applications.
giri::HTTPClient Class Reference

Simple HTTP Client. More...

#include <HTTPClient.h>

Inheritance diagram for giri::HTTPClient:
Collaboration diagram for giri::HTTPClient:

Public Types

enum class  Version : size_t { v_10 = 10 , v_11 = 11 }
 
- Public Types inherited from giri::Object< HTTPClient >
using SPtr = std::shared_ptr< HTTPClient >
 
using UPtr = std::unique_ptr< HTTPClient >
 
using WPtr = std::weak_ptr< HTTPClient >
 

Public Member Functions

 HTTPClient (bool ssl=false, HTTPClient::Version version=HTTPClient::Version::v_11, const std::string &userAgent="giris_supportlib_http_client", const std::string &contentType="application/x-www-form-urlencoded", const std::map< std::string, std::string > &customHead={})
 
void setUserAgentString (const std::string &userAgent)
 
void setContentType (const std::string &contentType)
 
void setVersion (const HTTPClient::Version v)
 
void setSSL (bool ssl)
 
void setCustomHead (std::map< std::string, std::string > customHead)
 
std::string getUserAgentString () const
 
std::string getContentType () const
 
HTTPClient::Version getVersion () const
 
bool getSSL () const
 
boost::system::error_code getError () const
 
std::map< std::string, std::string > getCustomHead () const
 
std::vector< char > HTTPGet (const std::string &host, const std::string &port, const std::string &target="/")
 
std::vector< char > HTTPDelete (const std::string &host, const std::string &port, const std::string &target="/")
 
std::vector< char > HTTPPost (const std::string &host, const std::string &port, const std::string &target, const std::vector< char > &data)
 
std::vector< char > HTTPPost (const std::string &host, const std::string &port, const std::string &target, const Blob &data)
 
std::vector< char > HTTPPut (const std::string &host, const std::string &port, const std::string &target, const std::vector< char > &data)
 
std::vector< char > HTTPPut (const std::string &host, const std::string &port, const std::string &target, const Blob &data)
 

Detailed Description

Simple HTTP Client.

Example Usage:

#include <HTTPClient.h>
#include <Blob.h>
#include <iostream>
#include <string>
using namespace giri;
int main()
{
HTTPClient::SPtr httpc = std::make_shared<HTTPClient>(true);
Blob b;
b = httpc->HTTPGet("www.google.at", "443");
std::cout << b.toString() << std::endl;
Blob postData;
postData.loadString("&Hello=World");
httpc->setSSL(false);
b = httpc->HTTPPost("ptsv2.com", "80", "/t/cu0dm-1579047674/post", postData);
std::cout << b.toString() << std::endl;
return EXIT_SUCCESS;
}
Binary large object implementation using std::vector<char>.
Simple HTTP Client implementation.
Namespace for giri's C++ support library.
Definition: Base64.h:47

Constructor & Destructor Documentation

◆ HTTPClient()

giri::HTTPClient::HTTPClient ( bool  ssl = false,
HTTPClient::Version  version = HTTPClient::Version::v_11,
const std::string &  userAgent = "giris_supportlib_http_client",
const std::string &  contentType = "application/x-www-form-urlencoded",
const std::map< std::string, std::string > &  customHead = {} 
)
inline

HTTPClient Constructor

Parameters
sslConfigure wether to use ssl or not. (defaults to false)
versionHTTP version to use (10 or 11). (defaults to 11)
userAgentUseragent string to be used for all requests. (defaults to "giris_supportlib_http_client")
contentTypeContent type to be used for all HTTPPost requests. (defaults to "application/x-www-form-urlencoded")
customHeadMapping containing custom fields to be added to the head of the HTTP requests. (useful for some REST APIs, defaults to empty)

Member Function Documentation

◆ getContentType()

std::string giri::HTTPClient::getContentType ( ) const
inline
Returns
Content type used for HTTPPost requests.

◆ getCustomHead()

std::map<std::string, std::string> giri::HTTPClient::getCustomHead ( ) const
inline
Returns
Mapping containing custom fields which are added to the head of the HTTP requests.

◆ getError()

boost::system::error_code giri::HTTPClient::getError ( ) const
inline
Returns
resulting error code of last request.

◆ getSSL()

bool giri::HTTPClient::getSSL ( ) const
inline
Returns
true if ssl is used, false otherwise.

◆ getUserAgentString()

std::string giri::HTTPClient::getUserAgentString ( ) const
inline
Returns
Useragent string used for all requests.

◆ getVersion()

HTTPClient::Version giri::HTTPClient::getVersion ( ) const
inline
Returns
HTTP version used by this client (10 or 11).

◆ HTTPDelete()

std::vector<char> giri::HTTPClient::HTTPDelete ( const std::string &  host,
const std::string &  port,
const std::string &  target = "/" 
)
inline

Performs a HTTPDelete request.

Parameters
hostHostname or IP of server.
portPort of server.
targetTargeted resource. (defaults to "/")
Returns
The result of the request.

◆ HTTPGet()

std::vector<char> giri::HTTPClient::HTTPGet ( const std::string &  host,
const std::string &  port,
const std::string &  target = "/" 
)
inline

Performs a HTTPGet request.

Parameters
hostHostname or IP of server.
portPort of server.
targetTargeted resource. (defaults to "/")
Returns
The result of the request.

◆ HTTPPost() [1/2]

std::vector<char> giri::HTTPClient::HTTPPost ( const std::string &  host,
const std::string &  port,
const std::string &  target,
const Blob data 
)
inline

Performs a HTTPPost request.

Parameters
hostHostname or IP of server.
portPort of server.
targetTargeted resource.
dataData to send via post request.
Returns
The result of the request.

◆ HTTPPost() [2/2]

std::vector<char> giri::HTTPClient::HTTPPost ( const std::string &  host,
const std::string &  port,
const std::string &  target,
const std::vector< char > &  data 
)
inline

Performs a HTTPPost request.

Parameters
hostHostname or IP of server.
portPort of server.
targetTargeted resource.
dataData to send via post request.
Returns
The result of the request.

◆ HTTPPut() [1/2]

std::vector<char> giri::HTTPClient::HTTPPut ( const std::string &  host,
const std::string &  port,
const std::string &  target,
const Blob data 
)
inline

Performs a HTTPPut request.

Parameters
hostHostname or IP of server.
portPort of server.
targetTargeted resource.
dataData to send via put request.
Returns
The result of the request.

◆ HTTPPut() [2/2]

std::vector<char> giri::HTTPClient::HTTPPut ( const std::string &  host,
const std::string &  port,
const std::string &  target,
const std::vector< char > &  data 
)
inline

Performs a HTTPPut request.

Parameters
hostHostname or IP of server.
portPort of server.
targetTargeted resource.
dataData to send via put request.
Returns
The result of the request.

◆ setContentType()

void giri::HTTPClient::setContentType ( const std::string &  contentType)
inline
Parameters
contentTypeContent type to be used for all HTTPPost requests.

◆ setCustomHead()

void giri::HTTPClient::setCustomHead ( std::map< std::string, std::string >  customHead)
inline
Parameters
customHeadMapping containing custom fields to be added to the head of the HTTP requests. (useful for some REST APIs)

◆ setSSL()

void giri::HTTPClient::setSSL ( bool  ssl)
inline
Parameters
sslConfigure wether to use ssl or not.

◆ setUserAgentString()

void giri::HTTPClient::setUserAgentString ( const std::string &  userAgent)
inline
Parameters
userAgentUseragent string to be used for all requests.

◆ setVersion()

void giri::HTTPClient::setVersion ( const HTTPClient::Version  v)
inline
Parameters
vHTTP version to use (10 or 11)

The documentation for this class was generated from the following file: