|
ASL
|
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.
The query parameters can be formatted with the Url::params() function, which makes sure they are URL encoded:
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):
In methods like post(), put() , patch(), that expect a body to be sent, this body can be:
application/x-www-form-urlencodedFile("/some/path.ext"), whose content will be sentThis would send a new profile photo by serializing a Var as JSON:
To download larger files you can use the download function, which saves directly to a file, instead of loading into a large memory buffer.
Using IPv6 addresses is supported with square brackets in the host part [ipv6]:port:
#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) | |