simple http request (temperature data) from mbed lpc1768 with application board to thingsboard device/dashboard
Fork of http-example by
Revision 31:cba9d9dea21a, committed 2018-06-29
- Comitter:
- daklowprofile
- Date:
- Fri Jun 29 03:19:58 2018 +0000
- Parent:
- 30:6b0698841e48
- Commit message:
- don't change anything
Changed in this revision
diff -r 6b0698841e48 -r cba9d9dea21a source/main-http-ipv6.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main-http-ipv6.cpp Fri Jun 29 03:19:58 2018 +0000 @@ -0,0 +1,49 @@ +#include "select-demo.h" + +#if DEMO == DEMO_HTTP_IPV6 + +#include "mbed.h" +#include "easy-connect.h" +#include "http_request.h" + +void dump_response(HttpResponse* res) { + printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +int main() { + // Connect to the network (see mbed_app.json for the connectivity method used) + NetworkInterface* network = easy_connect(true); + if (!network) { + printf("Cannot connect to the network, see serial output"); + return 1; + } + + // Do a GET request to icanhazip.com which returns the public IPv6 address for the device + // This page is only accessible over IPv6 + { + // By default the body is automatically parsed and stored in a buffer, this is memory heavy. + // To receive chunked response, pass in a callback as last parameter to the constructor. + HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://ipv6.icanhazip.com"); + + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; + } + + printf("\n----- HTTP GET response -----\n"); + dump_response(get_res); + + delete get_req; + } + + wait(osWaitForever); +} + +#endif
diff -r 6b0698841e48 -r cba9d9dea21a source/main-http-socket-reuse.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main-http-socket-reuse.cpp Fri Jun 29 03:19:58 2018 +0000 @@ -0,0 +1,87 @@ +#include "select-demo.h" + +#if DEMO == DEMO_HTTP_SOCKET_REUSE + +#include "mbed.h" +#include "easy-connect.h" +#include "http_request.h" + +void dump_response(HttpResponse* res) { + printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +int main() { + // Connect to the network (see mbed_app.json for the connectivity method used) + NetworkInterface* network = easy_connect(true); + if (!network) { + printf("Cannot connect to the network, see serial output"); + return 1; + } + + // Create a TCP socket + printf("\n----- Setting up TCP connection -----\n"); + + TCPSocket* socket = new TCPSocket(); + nsapi_error_t open_result = socket->open(network); + if (open_result != 0) { + printf("Opening TCPSocket failed... %d\n", open_result); + return 1; + } + + nsapi_error_t connect_result = socket->connect("httpbin.org", 80); + if (connect_result != 0) { + printf("Connecting over TCPSocket failed... %d\n", connect_result); + return 1; + } + + printf("Connected over TCP to httpbin.org:80\n"); + + // Do a GET request to httpbin.org + { + HttpRequest* get_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418"); + + // By default the body is automatically parsed and stored in a string, this is memory heavy. + // To receive chunked response, pass in a callback as third parameter to 'send'. + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; + } + + printf("\n----- HTTP GET response -----\n"); + dump_response(get_res); + + delete get_req; + } + + // POST request to httpbin.org + { + HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post"); + post_req->set_header("Content-Type", "application/json"); + + const char body[] = "{\"hello\":\"world\"}"; + + HttpResponse* post_res = post_req->send(body, strlen(body)); + if (!post_res) { + printf("HttpRequest failed (error code %d)\n", post_req->get_error()); + return 1; + } + + printf("\n----- HTTP POST response -----\n"); + dump_response(post_res); + + delete post_req; + } + + delete socket; + + wait(osWaitForever); +} + +#endif
diff -r 6b0698841e48 -r cba9d9dea21a source/main-http.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main-http.cpp Fri Jun 29 03:19:58 2018 +0000 @@ -0,0 +1,67 @@ +#include "select-demo.h" + +#if DEMO == DEMO_HTTP + +#include "mbed.h" +#include "easy-connect.h" +#include "http_request.h" + +void dump_response(HttpResponse* res) { + printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +int main() { + // Connect to the network (see mbed_app.json for the connectivity method used) + NetworkInterface* network = easy_connect(true); + if (!network) { + printf("Cannot connect to the network, see serial output"); + return 1; + } + + // Do a GET request to httpbin.org + { + // By default the body is automatically parsed and stored in a buffer, this is memory heavy. + // To receive chunked response, pass in a callback as last parameter to the constructor. + HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); + + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; + } + + printf("\n----- HTTP GET response -----\n"); + dump_response(get_res); + + delete get_req; + } + + // POST request to httpbin.org + { + HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); + post_req->set_header("Content-Type", "application/json"); + + const char body[] = "{\"hello\":\"world\"}"; + + HttpResponse* post_res = post_req->send(body, strlen(body)); + if (!post_res) { + printf("HttpRequest failed (error code %d)\n", post_req->get_error()); + return 1; + } + + printf("\n----- HTTP POST response -----\n"); + dump_response(post_res); + + delete post_req; + } + + wait(osWaitForever); +} + +#endif
diff -r 6b0698841e48 -r cba9d9dea21a source/main-https-chunked-request.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main-https-chunked-request.cpp Fri Jun 29 03:19:58 2018 +0000 @@ -0,0 +1,97 @@ +/** + * This is an example of doing chunked requests, where you do not need to load the full request body + * into memory. You do this by adding a callback to the `send` function of the HTTP/HTTPS request. + */ + +#include "select-demo.h" + +#if DEMO == DEMO_HTTPS_CHUNKED_REQUEST + +#include "mbed.h" +#include "https_request.h" +#include "easy-connect.h" + +/* List of trusted root CA certificates + * currently one: Comodo, the CA for reqres.in + * + * To add more root certificates, just concatenate them. + */ +const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" + "MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n" + "MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n" + "BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n" + "IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n" + "MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n" + "ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n" + "T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n" + "biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n" + "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n" + "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n" + "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n" + "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n" + "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n" + "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n" + "-----END CERTIFICATE-----\n"; + +void dump_response(HttpResponse* res) { + mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + mbedtls_printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +// Spread the message out over 3 different chunks +const char * chunks[] = { + "{\"message\":", + "\"this is an example", + " of chunked encoding\"}" +}; + +int chunk_ix = 0; + +// Callback function, grab the next chunk and return it +const void * get_chunk(size_t* out_size) { + // If you don't have any data left, set out_size to 0 and return a null pointer + if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) { + *out_size = 0; + return NULL; + } + const char *chunk = chunks[chunk_ix]; + *out_size = strlen(chunk); + chunk_ix++; + + return chunk; +} + +int main() { + NetworkInterface* network = easy_connect(true); + if (!network) { + return 1; + } + + // POST request to httpbin.org + { + HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users"); + post_req->set_debug(true); + post_req->set_header("Content-Type", "application/json"); + + // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked + HttpResponse* post_res = post_req->send(&get_chunk); + if (!post_res) { + printf("HttpsRequest failed (error code %d)\n", post_req->get_error()); + return 1; + } + + printf("\n----- HTTPS POST response -----\n"); + dump_response(post_res); + + delete post_req; + } + + wait(osWaitForever); +} + +#endif
diff -r 6b0698841e48 -r cba9d9dea21a source/main-https-socket-reuse.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main-https-socket-reuse.cpp Fri Jun 29 03:19:58 2018 +0000 @@ -0,0 +1,113 @@ +#include "select-demo.h" + +/** + * This demo shows how to re-use sockets, so the TLS handshake only has to happen once + */ + +#if DEMO == DEMO_HTTPS_SOCKET_REUSE + +#include "mbed.h" +#include "easy-connect.h" +#include "https_request.h" + +/* List of trusted root CA certificates + * currently one: Let's Encrypt, the CA for httpbin.org + * + * To add more root certificates, just concatenate them. + */ +const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" + "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" + "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" + "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" + "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" + "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" + "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" + "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" + "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" + "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" + "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" + "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" + "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" + "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" + "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" + "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" + "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" + "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\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 dump_response(HttpResponse* res) { + mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + mbedtls_printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +int main() { + NetworkInterface* network = easy_connect(true); + if (!network) { + return 1; + } + + // Create a TLS socket (which holds a TCPSocket) + printf("\n----- Setting up TLS connection -----\n"); + + TLSSocket* socket = new TLSSocket(network, "httpbin.org", 443, SSL_CA_PEM); + socket->set_debug(true); + if (socket->connect() != 0) { + printf("TLS Connect failed %d\n", socket->error()); + return 1; + } + + // GET request to httpbin.org + { + HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418"); + get_req->set_debug(true); + + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; + } + printf("\n----- HTTPS GET response -----\n"); + dump_response(get_res); + + delete get_req; + } + + // POST request to httpbin.org + { + HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://httpbin.org/post"); + post_req->set_debug(true); + post_req->set_header("Content-Type", "application/json"); + + const char body[] = "{\"hello\":\"world\"}"; + + HttpResponse* post_res = post_req->send(body, strlen(body)); + if (!post_res) { + printf("HttpRequest failed (error code %d)\n", post_req->get_error()); + return 1; + } + + printf("\n----- HTTPS POST response -----\n"); + dump_response(post_res); + + delete post_req; + } + + delete socket; + + wait(osWaitForever); +} + +#endif
diff -r 6b0698841e48 -r cba9d9dea21a source/main-https.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main-https.cpp Fri Jun 29 03:19:58 2018 +0000 @@ -0,0 +1,122 @@ +#include "select-demo.h" + +#if DEMO == DEMO_HTTPS + +#include "mbed.h" +#include "easy-connect.h" +#include "https_request.h" + +/* List of trusted root CA certificates + * currently two: GlobalSign, the CA for developer.mbed.org and Let's Encrypt, the CA for httpbin.org + * + * To add more root certificates, just concatenate them. + */ +const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" + "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n" + "A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n" + "b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n" + "MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n" + "YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n" + "aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n" + "jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n" + "xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n" + "1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n" + "snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n" + "U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n" + "9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n" + "BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n" + "AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n" + "yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n" + "38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n" + "AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n" + "DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n" + "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" + "-----END CERTIFICATE-----\n" + "-----BEGIN CERTIFICATE-----\n" + "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" + "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" + "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" + "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" + "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" + "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" + "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" + "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" + "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" + "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" + "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" + "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" + "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" + "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" + "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" + "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" + "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\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 dump_response(HttpResponse* res) { + mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); + + mbedtls_printf("Headers:\n"); + for (size_t ix = 0; ix < res->get_headers_length(); ix++) { + mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); + } + mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); +} + +int main() { + NetworkInterface* network = easy_connect(true); + if (!network) { + return 1; + } + + // GET request to developer.mbed.org + { + printf("\n----- HTTPS GET request -----\n"); + + HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt"); + get_req->set_debug(true); + + HttpResponse* get_res = get_req->send(); + if (!get_res) { + printf("HttpRequest failed (error code %d)\n", get_req->get_error()); + return 1; + } + printf("\n----- HTTPS GET response -----\n"); + dump_response(get_res); + + delete get_req; + } + + // POST request to httpbin.org + { + printf("\n----- HTTPS POST request -----\n"); + + HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); + post_req->set_debug(true); + post_req->set_header("Content-Type", "application/json"); + + const char body[] = "{\"hello\":\"world\"}"; + + HttpResponse* post_res = post_req->send(body, strlen(body)); + if (!post_res) { + printf("HttpRequest failed (error code %d)\n", post_req->get_error()); + return 1; + } + + printf("\n----- HTTPS POST response -----\n"); + dump_response(post_res); + + delete post_req; + } + + wait(osWaitForever); +} + +#endif