HTTP and HTTPS library for Mbed OS 5

Dependents:   MQTTGateway2 MQTTGatewayK64 http-example-wnc GuardRoom ... more

For the example program, see: sandbox/http-example.

This library is used to make HTTP and HTTPS calls from Mbed OS 5 applications.

HTTP Request API

NetworkInterface* network = /* obtain a NetworkInterface object */

const char body[] = "{\"hello\":\"world\"}";

HttpRequest* request = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
request->set_header("Content-Type", "application/json");
HttpResponse* response = request->send(body, strlen(body));
// if response is NULL, check response->get_error()

printf("status is %d - %s\n", response->get_status_code(), response->get_status_message());
printf("body is:\n%s\n", response->get_body_as_string().c_str());

delete request; // also clears out the response

HTTPS Request API

// pass in the root certificates that you trust, there is no central CA registry in Mbed OS
const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
    /* rest of the CA root certificates */;

NetworkInterface* network = /* obtain a NetworkInterface object */

const char body[] = "{\"hello\":\"world\"}";

HttpsRequest* request = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET "https://httpbin.org/status/418");
HttpResponse* response = request->send();
// if response is NULL, check response->get_error()

printf("status is %d - %s\n", response->get_status_code(), response->get_status_message());
printf("body is:\n%s\n", response->get_body().c_str());

delete request;

Note: You can get the root CA for a domain easily from Firefox. Click on the green padlock, click More information > Security > View certificate > Details. Select the top entry in the 'Certificate Hierarchy' and click Export.... This gives you a PEM file. Add the content of the PEM file to your root CA list (here's an image).

Mbed TLS Entropy configuration

If your target does not have a built-in TRNG, or other entropy sources, add the following macros to your mbed_app.json file to disable entropy:

{
    "macros": [
        "MBEDTLS_TEST_NULL_ENTROPY",
        "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES"
    ]
}

Note that this is not secure, and you should not deploy this device into production with this configuration.

Memory usage

Small requests where the body of the response is cached by the library (like the one found in main-http.cpp), require 4K of RAM. When the request is finished they require 1.5K of RAM, depending on the size of the response. This applies both to HTTP and HTTPS. If you need to handle requests that return a large response body, see 'Dealing with large body'.

HTTPS requires additional memory: on FRDM-K64F about 50K of heap space (at its peak). This means that you cannot use HTTPS on devices with less than 128K of memory, asyou also need to reserve memory for the stack and network interface.

Dealing with large response body

By default the library will store the full request body on the heap. This works well for small responses, but you'll run out of memory when receiving a large response body. To mitigate this you can pass in a callback as the last argument to the request constructor. This callback will be called whenever a chunk of the body is received. You can set the request chunk size in the HTTP_RECEIVE_BUFFER_SIZE macro (see mbed_lib.json for the definition) although it also depends on the buffer size ofthe underlying network connection.

void body_callback(const char* data, uint32_t data_len) {
    // do something with the data
}

HttpRequest* req = new HttpRequest(network, HTTP_GET, "http://pathtolargefile.com", &body_callback);
req->send(NULL, 0);

Dealing with a large request body

If you cannot load the full request into memory, you can pass a callback into the send function. Through this callback you can feed in chunks of the request body. This is very useful if you want to send files from a file system.

const void * get_chunk(uint32_t* out_size) {
    // set the value of out_size (via *out_size = 10) to the size of the buffer
    // return the buffer

    // if you don't have any more data, set *out_size to 0
}

HttpRequest* req = new HttpRequest(network, HTTP_POST, "http://my_api.com/upload");
req->send(callback(&get_chunk));

Socket re-use

By default the library opens a new socket per request. This is wasteful, especially when dealing with TLS requests. You can re-use sockets like this:

HTTP

TCPSocket* socket = new TCPSocket();

nsapi_error_t open_result = socket->open(network);
// check open_result

nsapi_error_t connect_result = socket->connect("httpbin.org", 80);
// check connect_result

// Pass in `socket`, instead of `network` as first argument
HttpRequest* req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418");

HTTPS

TLSSocket* socket = new TLSSocket();

nsapi_error_t r;
// make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
r = socket->open(network);
r = socket->set_root_ca_cert(SSL_CA_PEM);
r = socket->connect("httpbin.org", 443);

// Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument
HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");

Request logging

To make debugging easier you can log the raw request body that goes over the line. This also works with chunked encoding.

uint8_t *request_buffer = (uint8_t*)calloc(2048, 1);
req->set_request_log_buffer(request_buffer, 2048);

// after the request is done:
printf("\n----- Request buffer -----\n");
for (size_t ix = 0; ix < req->get_request_log_buffer_length(); ix++) {
    printf("%02x ", request_buffer[ix]);
}
printf("\n");

Integration tests

Integration tests are located in the TESTS folder and are ran through Greentea. Instructions on how to run the tests are in http-example.

Mbed OS 5.10 or lower

If you want to use this library on Mbed OS 5.10 or lower, you need to add the TLSSocket library to your project. This library is included in Mbed OS 5.11 and up.

Tested on

  • K64F with Ethernet.
  • NUCLEO_F411RE with ESP8266.
  • ODIN-W2 with WiFi.
  • K64F with Atmel 6LoWPAN shield.
  • DISCO-L475VG-IOT01A with WiFi.
  • Mbed Simulator.

Files at this revision

API Documentation at this revision

Comitter:
Jan Jongboom
Date:
Fri Jan 04 13:27:32 2019 +0100
Parent:
33:5b2869cc8934
Commit message:
Add integration tests

Changed in this revision

TESTS/COMMON/test_setup.h Show annotated file Show diff for this revision Revisions of this file
TESTS/tests/integration/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TESTS/COMMON/test_setup.h	Fri Jan 04 13:27:32 2019 +0100
@@ -0,0 +1,101 @@
+#ifndef _MBED_HTTP_TEST_SETUP_H_
+#define _MBED_HTTP_TEST_SETUP_H_
+
+#include "mbed.h"
+#include "NetworkInterface.h"
+
+/**
+ * Connect to the network using the default networking interface,
+ * you can also swap this out with a driver for a different networking interface
+ * if you use WiFi: see mbed_app.json for the credentials
+ */
+NetworkInterface *connect_to_default_network_interface() {
+    NetworkInterface* network = NetworkInterface::get_default_instance();
+
+    if (!network) {
+        return NULL;
+    }
+
+    nsapi_error_t connect_status = network->connect();
+
+    if (connect_status != NSAPI_ERROR_OK) {
+        return NULL;
+    }
+
+    return network;
+}
+
+
+/* List of trusted root CA certificates
+ *
+ * - Amazon, the CA for os.mbed.com
+ * - Let's Encrypt, the CA for httpbin.org
+ * - Comodo, the CA for reqres.in
+ *
+ * To add more root certificates, just concatenate them.
+ */
+const char SSL_CA_PEM[] =  "-----BEGIN CERTIFICATE-----\n"
+    "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n"
+    "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n"
+    "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n"
+    "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n"
+    "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n"
+    "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n"
+    "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n"
+    "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n"
+    "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n"
+    "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n"
+    "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n"
+    "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n"
+    "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n"
+    "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n"
+    "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n"
+    "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n"
+    "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n"
+    "rqXRfboQnoZsG4q5WTP468SQvvG5\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"
+    "-----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";
+
+#endif // MBED_HTTP_TEST_SETUP_H_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TESTS/tests/integration/main.cpp	Fri Jan 04 13:27:32 2019 +0100
@@ -0,0 +1,266 @@
+/*
+ * PackageLicenseDeclared: Apache-2.0
+ * Copyright (c) 2018 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "http_request.h"
+#include "https_request.h"
+#include "test_setup.h"
+#include "utest/utest.h"
+#include "unity/unity.h"
+#include "greentea-client/test_env.h"
+
+using namespace utest::v1;
+
+static NetworkInterface *network;
+
+static void setup_verify_network() {
+    if (!network) network = connect_to_default_network_interface();
+    TEST_ASSERT_NOT_NULL(network);
+}
+
+// verifies that the header is present and has a certain value
+static void assert_header(HttpResponse *res, const char *header, const char *value) {
+    bool headerPresent = false;
+    for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
+        if (res->get_headers_fields()[ix]->compare(header)) {
+            headerPresent = true;
+
+            TEST_ASSERT(res->get_headers_values()[ix]->compare(value));
+        }
+    }
+    TEST_ASSERT_EQUAL(true, headerPresent);
+}
+
+static control_t http_get(const size_t call_count) {
+    setup_verify_network();
+
+    HttpRequest *req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418");
+
+    HttpResponse* res = req->send();
+    TEST_ASSERT(res);
+    TEST_ASSERT_EQUAL(418, res->get_status_code());
+
+    delete req;
+
+    return CaseNext;
+}
+
+static control_t http_post(const size_t call_count) {
+    setup_verify_network();
+
+    HttpRequest* req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
+    req->set_header("Content-Type", "application/json");
+
+    const char body[] = "{\"mykey\":\"mbedvalue\"}";
+
+    HttpResponse* res = req->send(body, strlen(body));
+    TEST_ASSERT(res);
+    TEST_ASSERT_EQUAL(200, res->get_status_code());
+
+    // verify that the Content-Type header is present, and set to application/json
+    assert_header(res, "Content-Type", "application/json");
+
+    // verify that both the key and value are present in the response
+    TEST_ASSERT(res->get_body_length() > 0);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("mykey"), string::npos);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("mbedvalue"), string::npos);
+
+    delete req;
+
+    return CaseNext;
+}
+
+static control_t http_socket_reuse(const size_t call_count) {
+    setup_verify_network();
+
+    TCPSocket socket;
+    nsapi_error_t open_result = socket.open(network);
+    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, open_result);
+
+    nsapi_error_t connect_result = socket.connect("httpbin.org", 80);
+    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, connect_result);
+
+    {
+        HttpRequest *req = new HttpRequest(&socket, HTTP_GET, "http://httpbin.org/status/404");
+
+        HttpResponse* res = req->send();
+        TEST_ASSERT(res);
+        TEST_ASSERT_EQUAL(404, res->get_status_code());
+
+        delete req;
+    }
+
+    {
+        HttpRequest *req = new HttpRequest(&socket, HTTP_GET, "http://httpbin.org/status/403");
+
+        HttpResponse* res = req->send();
+        TEST_ASSERT(res);
+        TEST_ASSERT_EQUAL(403, res->get_status_code());
+
+        delete req;
+    }
+
+    return CaseNext;
+}
+
+static control_t https_get(const size_t call_count) {
+    setup_verify_network();
+
+    HttpsRequest *req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt");
+
+    HttpResponse* res = req->send();
+    TEST_ASSERT(res);
+    TEST_ASSERT_EQUAL(200, res->get_status_code());
+    TEST_ASSERT(res->get_body_length() > 0);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("Hello world!"), string::npos);
+
+    delete req;
+
+    return CaseNext;
+}
+
+static control_t https_post(const size_t call_count) {
+    setup_verify_network();
+
+    HttpsRequest* req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post");
+    req->set_header("Content-Type", "application/json");
+
+    const char body[] = "{\"myhttpskey\":\"janjanjan\"}";
+
+    HttpResponse* res = req->send(body, strlen(body));
+    TEST_ASSERT(res);
+    TEST_ASSERT_EQUAL(200, res->get_status_code());
+
+    // verify that the Content-Type header is present, and set to application/json
+    assert_header(res, "Content-Type", "application/json");
+
+    // verify that both the key and value are present in the response
+    TEST_ASSERT(res->get_body_length() > 0);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("myhttpskey"), string::npos);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("janjanjan"), string::npos);
+
+    delete req;
+
+    return CaseNext;
+}
+
+static control_t https_socket_reuse(const size_t call_count) {
+    setup_verify_network();
+
+    TLSSocket socket;
+    nsapi_error_t open_result = socket.open(network);
+    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, open_result);
+
+    nsapi_error_t ca_result = socket.set_root_ca_cert(SSL_CA_PEM);
+    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, ca_result);
+
+    nsapi_error_t connect_result = socket.connect("httpbin.org", 443);
+    TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, connect_result);
+
+    {
+        HttpsRequest *req = new HttpsRequest(&socket, HTTP_GET, "http://httpbin.org/status/404");
+
+        HttpResponse* res = req->send();
+        TEST_ASSERT(res);
+        TEST_ASSERT_EQUAL(404, res->get_status_code());
+
+        delete req;
+    }
+
+    {
+        HttpsRequest *req = new HttpsRequest(&socket, HTTP_GET, "http://httpbin.org/status/403");
+
+        HttpResponse* res = req->send();
+        TEST_ASSERT(res);
+        TEST_ASSERT_EQUAL(403, res->get_status_code());
+
+        delete req;
+    }
+
+    return CaseNext;
+}
+
+// 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(uint32_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;
+}
+
+static control_t chunked_request(const size_t call_count) {
+    setup_verify_network();
+
+    HttpsRequest *req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users");
+    req->set_header("Content-Type", "application/json");
+
+    HttpResponse* res = req->send(&get_chunk);
+    TEST_ASSERT(res);
+    TEST_ASSERT_EQUAL(201, res->get_status_code());
+    TEST_ASSERT(res->get_body_length() > 0);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("message"), string::npos);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("this is an example of chunked encoding"), string::npos);
+    TEST_ASSERT_NOT_EQUAL(res->get_body_as_string().find("createdAt"), string::npos);
+
+    delete req;
+
+    return CaseNext;
+}
+
+utest::v1::status_t greentea_setup(const size_t number_of_cases) {
+    GREENTEA_SETUP(1*60, "default_auto");
+    return greentea_test_setup_handler(number_of_cases);
+}
+
+Case cases[] = {
+    Case("http get", http_get),
+    Case("http post", http_post),
+    Case("http socket reuse", http_socket_reuse),
+    Case("https get", https_get),
+    Case("https post", https_post),
+    Case("https socket reuse", https_socket_reuse),
+    Case("chunked request", chunked_request)
+};
+
+Specification specification(greentea_setup, cases);
+
+void blink_led() {
+    static DigitalOut led(LED1);
+    led = !led;
+}
+
+int main() {
+    Ticker t;
+    t.attach(blink_led, 0.5);
+
+    return !Harness::run(specification);
+}