class::inet::HttpClient

class inet::HttpClient
  : public inet::Http

The HTTP client class can be used to execute HTTP requests to HTTP servers.

This first example will download a file from the internet.

#include <sapi/inet.hpp>
#include <sapi/sys.hpp>

SecureSocket socket; //or just use Socket for http
HttpClient http_client(socket);
File response;
response.create("/home/response.html");
http.get("https://stratifylabs.co", response);
response.close();

Summary

HttpClient(

Socket & socket
)

int close_connection()

int get(

const arg::UrlEncodedString url,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

int head(

const arg::UrlEncodedString url
)

const var::String & header() const

var::Vector< HttpHeaderPair > & header_request_pairs()

const var::Vector< HttpHeaderPair > & header_request_pairs() const

var::Vector< HttpHeaderPair > & header_response_pairs()

const var::Vector< HttpHeaderPair > & header_response_pairs() const

bool is_follow_redirects() const

bool is_keep_alive() const

int patch(

const arg::UrlEncodedString url,
const arg::HttpStringToPost request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

int patch(

const arg::UrlEncodedString url,
const arg::SourceFile & request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

int post(

const arg::UrlEncodedString url,
const arg::HttpStringToPost request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

int post(

const arg::UrlEncodedString url,
const arg::SourceFile & request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

int put(

const arg::UrlEncodedString url,
const arg::HttpStringToPost request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

int put(

const arg::UrlEncodedString url,
const arg::SourceFile & request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

Http & set_chunked_transfer_encoding_enabled(

bool value
)

void set_follow_redirects(

bool value
)

void set_keep_alive(

bool value
)

Http & set_transfer_size(

u32 value
)

int status_code() const

u32 transfer_size() const

enum error

Members

HttpClient(

Socket & socket
)

Constructs a new HttpClient object.

Parameters

  • socket A reference to the socket to use

The socket can be Socket for http or SecureSocket for https connections. The OS build must support https for SecureSocket to work correctly.


int close_connection()

Closes the socket.

The connection is automatically closed unless set_keep_alive() has been executed.


int get(

const arg::UrlEncodedString url,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)

Executes an HTTP GET request.

Parameters

  • url The URL to get (like https://stratifylabs.co)

  • response An open file that can accept the data that is returned

  • progress_callback An optional callback that can be used to update the user on the progress of the download

If the URL uses https, then this object should be constructed with a SecureSocket rather than a simple Socket.

#include <sapi/inet.hpp>
#include <sapi/sys.hpp>

SecureSocket socket; //or just use Socket for http
HttpClient http_client(socket);
DataFile response(File::APPEND);
http.get("https://stratifylabs.co", response);

int head(

const arg::UrlEncodedString url
)

Executes a HEAD request.

Parameters

  • url target URL for request.

const var::String & header() const

Returns a reference to the header that is returned by the request.


var::Vector< HttpHeaderPair > & header_request_pairs()

const var::Vector< HttpHeaderPair > & header_request_pairs() const

var::Vector< HttpHeaderPair > & header_response_pairs()

const var::Vector< HttpHeaderPair > & header_response_pairs() const

bool is_follow_redirects() const

bool is_keep_alive() const

Returns true if the connection should be kept alive.


int patch(

const arg::UrlEncodedString url,
const arg::HttpStringToPost request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)


int patch(

const arg::UrlEncodedString url,
const arg::SourceFile & request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)


int post(

const arg::UrlEncodedString url,
const arg::HttpStringToPost request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)


int post(

const arg::UrlEncodedString url,
const arg::SourceFile & request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)


int put(

const arg::UrlEncodedString url,
const arg::HttpStringToPost request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)


int put(

const arg::UrlEncodedString url,
const arg::SourceFile & request,
const arg::DestinationFile & response,
const sys::ProgressCallback * progress_callback
)


Http & set_chunked_transfer_encoding_enabled(

bool value
)

Sets the transfer encoding to chunked.

Parameters

  • value true if the transfer is to be chunked.

void set_follow_redirects(

bool value
)


void set_keep_alive(

bool value
)

Keeps the connection alive between requests.

Parameters

  • value If true, the connection is kept alive between requests

Http & set_transfer_size(

u32 value
)

Sets the transfer chunk size.

Parameters

  • value Transfer size in bytes

This sets the maximum chunk size used when downloading a file. This amount will be read from the socket then written to the file before another chunkc is ready from the socket.


int status_code() const

Returns the status code of the last request.

The status code will be 200 for a successful request.


u32 transfer_size() const

Returns the current transfer chunk size.


enum error
  • NONE No Errors
  • FAILED_TO_CREATE_SOCKET Failed to create a socket (1)
  • FAILED_TO_CONNECT_TO_SOCKET Failed to connect to socket (2)
  • FAILED_TO_WRITE_HEADER Failed to write request header (3)
  • FAILED_TO_WRITE_DATA Failed to write data (4)
  • FAILED_TO_WRITE_INCOMING_DATA_TO_FILE Failed write incoming data to file provided (5)
  • FAILED_TO_FIND_ADDRESS Failed to find IP address of URL (6)
  • FAILED_TO_GET_STATUS_CODE Failed to get a status code in the HTTP response (7)
  • FAILED_TO_GET_HEADER Failed to receive the header (8)
  • FAILED_WRONG_DOMAIN

List of values for error_number() when using the HttpClient class.


X

Thanks for Coming!

Subscribe to news and updates