ASL
Http Class Reference

Detailed Description

This class contains the basic HTTP/HTTPS client functionality.

Requests are made with the static functions get(), put(), post(), etc. which will return an HttpResponse with the response headers, body and status.

This code would get local wheather information in JSON format from a web service after first querying our geographical location from another server. You need a registered user key to access this service.

auto loc = Http::get("http://ipinfo.io/").json()["loc"].toString().split(",");
auto response = Http::get("http://api.openweathermap.org/data/2.5/weather?lat=" + loc[0] + "&lon=" + loc[1] + "&APPID=" +
key);
if (!response.ok()) // check if the request succeeded
return;
Var data = response.json();
String city = data["name"];
float temperature = data["main"]["temp"];
float pressure = data["main"]["pressure"];
static HttpResponse get(const String &url, const Dic<> &headers=Dic<>())
Sends an HTTP GET request for the given url and returns the response.
Definition: Http.h:535
Var json() const
Returns the message body interpreted as JSON.

The query parameters can be formatted with the Url::params() function, which makes sure they are URL encoded:

Http::get("http://api.openweathermap.org/data/2.5/weather?" + Url::params({{"lat", loc[0]}, {"lon", loc[1]}, {"APPID", key}}));
Dic params() const
Returns the parameters in the query string as a map of names and values.
Definition: Http.h:54

HTTPS client functionality currently does not check the server's certificate but the communication is encrypted anyway. This would upload a file to a Dropbox app (provided we have an authorization key that goes in the Authorization request header):

String content = "My New File!\n";
auto res = Http::post("https://content.dropboxapi.com/1/files_put/auto/myfile.txt", content, Dic<>("Authorization", "Bearer ..."));
static HttpResponse post(const String &url, const T &body, const Dic<> &headers=Dic<>())
Sends an HTTP POST request for the given url with the given body and returns the response; The body c...
Definition: Http.h:553

In methods like post(), put() , patch(), that expect a body to be sent, this body can be:

  • A String, for text
  • A ::ByteArray, for binary data
  • A Var, which will be serialized as JSON, or as URL-encoded if the content type is set to application/x-www-form-urlencoded
  • A File, like File("/some/path.ext"), whose content will be sent

This would send a new profile photo by serializing a Var as JSON:

auto res = Http::post("https://api.dropboxapi.com/2/account/set_profile_photo", Var{
{"photo", {
{".tag", "base64_data"},
{"base64_data", encodeBase64(File("pic.jpg").content())}
}}}, Dic<>("Authorization", "Bearer ..."));

To download larger files you can use the download function, which saves directly to a file, instead of loading into a large memory buffer.

Http::download("http://someserver/some/large/file.zip", "./file.zip", [=](const HttpStatus& s) {
printf("\r%i / %i bytes (%.0f %%) ", s.received, s.totalReceive, 100.0 * s.received / s.totalReceive);
});
static bool download(const String &url, const String &path, const Function< void, const HttpStatus & > &f=Progress(), const Dic<> &headers=Dic<>())
Downloads the given URL to the specified local path, optionally notifying progress.

Using IPv6 addresses is supported with square brackets in the host part [ipv6]:port:

auto res = Http::get("http://[::1]:80/path");

#include <Http.h>

Public Types

typedef Function< void, const HttpStatus & > Progress
 Type for HTTP progress callbacks.
 

Static Public Member Functions

static HttpResponse request (HttpRequest &req)
 Sends an HTTP request and returns the response.
 
static HttpResponse get (const String &url, const Dic<> &headers=Dic<>())
 Sends an HTTP GET request for the given url and returns the response.
 
template<class T >
static HttpResponse put (const String &url, const T &body, const Dic<> &headers=Dic<>())
 Sends an HTTP PUT request for the given url with the given data and returns the response.
 
template<class T >
static HttpResponse post (const String &url, const T &body, const Dic<> &headers=Dic<>())
 Sends an HTTP POST request for the given url with the given body and returns the response; The body can be a String, a ::ByteArray, a Var (sent as JSON) or a File; If body is a File and headers contain Content-Type multipart/form-data, the file will be uploaded as an HTML form's file item.
 
static HttpResponse delet (const String &url, const Dic<> &headers=Dic<>())
 Sends an HTTP DELETE request for the given url and returns the response.
 
template<class T >
static HttpResponse patch (const String &url, const T &body, const Dic<> &headers=Dic<>())
 Sends an HTTP PATCH request for the given url with the given data and returns the response.
 
static bool download (const String &url, const String &path, const Function< void, const HttpStatus & > &f=Progress(), const Dic<> &headers=Dic<>())
 Downloads the given URL to the specified local path, optionally notifying progress.
 
static bool upload (const String &url, const String &path, const Dic<> &headers=Dic<>(), const Function< void, const HttpStatus & > &f=Progress())
 Uploads (POST) to the given URL the file specified, optionally notifying progress (uses multipart/form-data unless a specific Content-Type is given)
 

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