HTTP Client
HTTPClient
class is a simple library that implements the logic for interacting with HTTP servers.
Assembly
http_client.h
Summary
Types |
---|
Http_Response |
Constructors |
---|
HTTPClient - HTTPClient(http_method method, const char* url, Callback<void(const char *at, size_t length)> body_callback = 0) |
HTTPClient - HTTPClient(const char* ssl_ca_pem, http_method method, const char* url, Callback<void(const char *at, size_t length)> body_callback = 0) |
Destructors |
---|
~HTTPClient - virtual ~HTTPClient(void) |
Methods |
---|
send - const Http_Response* send(const void* body = NULL, int body_size = 0) |
set_header - void set_header(const char* key, const char* value) |
get_error - nsapi_error_t get_error() |
Types
Http_Response
typedef struct
{
int status_code;
int body_length;
const char* status_message;
const char* body;
const KEYVALUE* headers;
} Http_Response;
Struct that provide the information of data receive response.
Constructor
HTTPClient
HTTPClient(http_method method, const char* url, Callback<void(const char *at, size_t length)> body_callback = 0)
Parameters
Type Name Description http_method method Http method to use const char* url The url the request is send to Callback body_callback The callback function on which to retrieve chunks of the response body
HTTPClient
HTTPClient(const char* ssl_ca_pem, http_method method, const char* url, Callback<void(const char *at, size_t length)> body_callback = 0)
Parameters
Type Name Description const char* ssl_ca_pem The message of the trusted CAs http_method method Http method to use const char* url The url the request is send to Callback body_callback The callback function on which to retrieve chunks of the response body
Destructors
~HTTPClient
virtual ~HTTPClient(void)
Destructor of
HTTPClient
class
Methods
send
const Http_Response* send(const void* body = NULL, int body_size = 0)
Execute the HTTP request
Parameters
Type Name Description const char * body Pointer to the request body int body_size Size of request body Return value
Returns an HttpResponse pointer on success, or NULL on failure
set_header
void set_header(const char* key, const char* value)
Send a header for the HTTP request
Parameters
Type Name Description const char* key the header key const char* value the header value Return value
void
get_error
nsapi_error_t get_error()
Get the error code, when send() fails, this error is set
Parameters
None
Return value
Returns the error code
Sample code
#include "AZ3166WiFi.h"
#include "http_client.h"
static bool hasWifi;
const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG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5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n"
"MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n"
"Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n"
"AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n"
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n"
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n"
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n"
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n"
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
"-----END CERTIFICATE-----\n";
void InitWiFi()
{
if (WiFi.begin() == WL_CONNECTED)
{
IPAddress ip = WiFi.localIP();
Screen.print(1, ip.get_address());
hasWifi = true;
}
else
{
Screen.print(1, "No Wi-Fi ");
}
}
void http_test()
{
Screen.clean();
Screen.print(0, "HTTP Client");
HTTPClient *httpClient = new HTTPClient(SSL_CA_PEM, HTTP_GET, "https://httpbin.org/status/418");
const Http_Response* result = httpClient->send();
if (result == NULL)
{
Screen.print(1, "Failed");
Serial.print("Error Code: ");
Serial.println(httpClient->get_error());
}
else
{
Screen.print(1, "Succeed");
Serial.println("Body");
Serial.println(result->body);
}
delete httpClient;
}
void setup() {
hasWifi = false;
InitWiFi();
if(hasWifi)
{
http_test();
}
}
void loop() {
}