HTTP and HTTPS example application for Mbed OS 5

Dependencies:   mbed-http

This application demonstrates how to make HTTP and HTTPS requests and parse the response from Mbed OS 5.

It consists of six example applications, which you can select in source/select-demo.h:

Response parsing is done through nodejs/http-parser.

Note: HTTPS requests do not work on targets with less than 128K of RAM due to the size of the TLS handshake. For more background see mbed-http.

To build

  1. If you're using WiFi, specify the credentials in mbed_app.json.
  2. Build the project in the online compiler or using Mbed CLI.
  3. Flash the project to your development board.
  4. Attach a serial monitor to your board to see the debug messages.

Defining the network interface

This application uses the on-board network interface for your board. If you use an external network interface (f.e. a WiFi module) you need to add the driver to this project. Then, open network-helper.h and specify which network driver to use.

More information is in the Mbed OS documentation under IP Networking.

Entropy (or lack thereof)

On all platforms that do not have the TRNG feature, the application is compiled without TLS entropy sources. This means that your code is inherently unsafe and should not be deployed to any production systems. To enable entropy, remove the MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES and MBEDTLS_TEST_NULL_ENTROPY macros from mbed_app.json.

Flash size

Default flash size for HTTPS is very large, as the application is loading the default Mbed TLS configuration. To use a more optimized version, you can disable unused cypher suites and other Mbed TLS features with a custom configuration file. Create a new configuration file, then add in mbed_app.json:

"MBEDTLS_CONFIG_FILE=\"mbedtls_config.h\""

to the macros array.

Running tests

You can run the integration tests from this project via Mbed CLI.

  1. In select-demo.h set the DEMO macro to DEMO_TESTS.
  2. Set your WiFi credentials in mbed_app.json.
  3. Then run the tests via:

$ mbed test -v -n mbed-http-tests-tests-*

Tested on

  • K64F with Ethernet.
  • NUCLEO_F411RE with ESP8266 (not working on Mbed OS 5.12+)
  • ODIN-W2 with WiFi.
  • K64F with Atmel 6LoWPAN shield.
  • DISCO-L475VG-IOT01A with WiFi (requires the wifi-ism43362 driver).
Committer:
Jan Jongboom
Date:
Fri Jan 04 13:32:26 2019 +0100
Revision:
35:4b847971db1b
Parent:
31:66704f6f17c5
Update to Mbed OS 5.11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jan Jongboom 25:a8be9f3a530c 1 /**
Jan Jongboom 25:a8be9f3a530c 2 * This is an example of doing chunked requests, where you do not need to load the full request body
Jan Jongboom 25:a8be9f3a530c 3 * into memory. You do this by adding a callback to the `send` function of the HTTP/HTTPS request.
Jan Jongboom 25:a8be9f3a530c 4 */
Jan Jongboom 25:a8be9f3a530c 5
Jan Jongboom 25:a8be9f3a530c 6 #include "select-demo.h"
Jan Jongboom 25:a8be9f3a530c 7
Jan Jongboom 25:a8be9f3a530c 8 #if DEMO == DEMO_HTTPS_CHUNKED_REQUEST
Jan Jongboom 25:a8be9f3a530c 9
Jan Jongboom 25:a8be9f3a530c 10 #include "mbed.h"
Jan Jongboom 31:66704f6f17c5 11 #include "mbed_trace.h"
Jan Jongboom 25:a8be9f3a530c 12 #include "https_request.h"
Jan Jongboom 30:4825e4f38844 13 #include "network-helper.h"
Jan Jongboom 25:a8be9f3a530c 14
Jan Jongboom 25:a8be9f3a530c 15 /* List of trusted root CA certificates
Jan Jongboom 25:a8be9f3a530c 16 * currently one: Comodo, the CA for reqres.in
Jan Jongboom 25:a8be9f3a530c 17 *
Jan Jongboom 25:a8be9f3a530c 18 * To add more root certificates, just concatenate them.
Jan Jongboom 25:a8be9f3a530c 19 */
Jan Jongboom 25:a8be9f3a530c 20 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
Jan Jongboom 25:a8be9f3a530c 21 "MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n"
Jan Jongboom 25:a8be9f3a530c 22 "MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n"
Jan Jongboom 25:a8be9f3a530c 23 "BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n"
Jan Jongboom 25:a8be9f3a530c 24 "IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n"
Jan Jongboom 25:a8be9f3a530c 25 "MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n"
Jan Jongboom 25:a8be9f3a530c 26 "ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n"
Jan Jongboom 25:a8be9f3a530c 27 "T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n"
Jan Jongboom 25:a8be9f3a530c 28 "biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n"
Jan Jongboom 25:a8be9f3a530c 29 "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n"
Jan Jongboom 25:a8be9f3a530c 30 "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n"
Jan Jongboom 25:a8be9f3a530c 31 "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n"
Jan Jongboom 25:a8be9f3a530c 32 "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n"
Jan Jongboom 25:a8be9f3a530c 33 "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n"
Jan Jongboom 25:a8be9f3a530c 34 "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n"
Jan Jongboom 25:a8be9f3a530c 35 "-----END CERTIFICATE-----\n";
Jan Jongboom 25:a8be9f3a530c 36
Jan Jongboom 25:a8be9f3a530c 37 void dump_response(HttpResponse* res) {
Jan Jongboom 31:66704f6f17c5 38 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
Jan Jongboom 25:a8be9f3a530c 39
Jan Jongboom 31:66704f6f17c5 40 printf("Headers:\n");
Jan Jongboom 25:a8be9f3a530c 41 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
Jan Jongboom 31:66704f6f17c5 42 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
Jan Jongboom 25:a8be9f3a530c 43 }
Jan Jongboom 31:66704f6f17c5 44 printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
Jan Jongboom 25:a8be9f3a530c 45 }
Jan Jongboom 25:a8be9f3a530c 46
Jan Jongboom 25:a8be9f3a530c 47 // Spread the message out over 3 different chunks
Jan Jongboom 25:a8be9f3a530c 48 const char * chunks[] = {
Jan Jongboom 25:a8be9f3a530c 49 "{\"message\":",
Jan Jongboom 25:a8be9f3a530c 50 "\"this is an example",
Jan Jongboom 25:a8be9f3a530c 51 " of chunked encoding\"}"
Jan Jongboom 25:a8be9f3a530c 52 };
Jan Jongboom 25:a8be9f3a530c 53
Jan Jongboom 25:a8be9f3a530c 54 int chunk_ix = 0;
Jan Jongboom 25:a8be9f3a530c 55
Jan Jongboom 25:a8be9f3a530c 56 // Callback function, grab the next chunk and return it
Jan Jongboom 31:66704f6f17c5 57 const void * get_chunk(uint32_t* out_size) {
Jan Jongboom 25:a8be9f3a530c 58 // If you don't have any data left, set out_size to 0 and return a null pointer
Jan Jongboom 25:a8be9f3a530c 59 if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) {
Jan Jongboom 25:a8be9f3a530c 60 *out_size = 0;
Jan Jongboom 25:a8be9f3a530c 61 return NULL;
Jan Jongboom 25:a8be9f3a530c 62 }
Jan Jongboom 25:a8be9f3a530c 63 const char *chunk = chunks[chunk_ix];
Jan Jongboom 25:a8be9f3a530c 64 *out_size = strlen(chunk);
Jan Jongboom 25:a8be9f3a530c 65 chunk_ix++;
Jan Jongboom 25:a8be9f3a530c 66
Jan Jongboom 25:a8be9f3a530c 67 return chunk;
Jan Jongboom 25:a8be9f3a530c 68 }
Jan Jongboom 25:a8be9f3a530c 69
Jan Jongboom 25:a8be9f3a530c 70 int main() {
Jan Jongboom 30:4825e4f38844 71 NetworkInterface* network = connect_to_default_network_interface();
Jan Jongboom 25:a8be9f3a530c 72 if (!network) {
Jan Jongboom 30:4825e4f38844 73 printf("Cannot connect to the network, see serial output\n");
Jan Jongboom 25:a8be9f3a530c 74 return 1;
Jan Jongboom 25:a8be9f3a530c 75 }
Jan Jongboom 25:a8be9f3a530c 76
Jan Jongboom 31:66704f6f17c5 77 mbed_trace_init();
Jan Jongboom 31:66704f6f17c5 78
Jan Jongboom 31:66704f6f17c5 79 // This example also logs the raw request, you can do this by calling 'set_request_log_buffer' on the request
Jan Jongboom 31:66704f6f17c5 80 uint8_t *request_buffer = (uint8_t*)calloc(2048, 1);
Jan Jongboom 31:66704f6f17c5 81
Jan Jongboom 31:66704f6f17c5 82 // POST request to reqres.in
Jan Jongboom 25:a8be9f3a530c 83 {
Jan Jongboom 25:a8be9f3a530c 84 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users");
Jan Jongboom 25:a8be9f3a530c 85 post_req->set_header("Content-Type", "application/json");
Jan Jongboom 31:66704f6f17c5 86 post_req->set_request_log_buffer(request_buffer, 2048);
Jan Jongboom 25:a8be9f3a530c 87
Jan Jongboom 25:a8be9f3a530c 88 // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked
Jan Jongboom 25:a8be9f3a530c 89 HttpResponse* post_res = post_req->send(&get_chunk);
Jan Jongboom 25:a8be9f3a530c 90 if (!post_res) {
Jan Jongboom 25:a8be9f3a530c 91 printf("HttpsRequest failed (error code %d)\n", post_req->get_error());
Jan Jongboom 25:a8be9f3a530c 92 return 1;
Jan Jongboom 25:a8be9f3a530c 93 }
Jan Jongboom 25:a8be9f3a530c 94
Jan Jongboom 31:66704f6f17c5 95 // Log the raw request that went over the line (if you decode the hex you can see the chunked parts)
Jan Jongboom 31:66704f6f17c5 96 // e.g. in Node.js (take the output from below):
Jan Jongboom 31:66704f6f17c5 97 // '50 4f 53 54 20'.split(' ').map(c=>parseInt(c,16)).map(c=>String.fromCharCode(c)).join('')
Jan Jongboom 31:66704f6f17c5 98 printf("\n----- Request buffer -----\n");
Jan Jongboom 31:66704f6f17c5 99 for (size_t ix = 0; ix < post_req->get_request_log_buffer_length(); ix++) {
Jan Jongboom 31:66704f6f17c5 100 printf("%02x ", request_buffer[ix]);
Jan Jongboom 31:66704f6f17c5 101 }
Jan Jongboom 31:66704f6f17c5 102 printf("\n");
Jan Jongboom 31:66704f6f17c5 103
Jan Jongboom 25:a8be9f3a530c 104 printf("\n----- HTTPS POST response -----\n");
Jan Jongboom 25:a8be9f3a530c 105 dump_response(post_res);
Jan Jongboom 25:a8be9f3a530c 106
Jan Jongboom 25:a8be9f3a530c 107 delete post_req;
Jan Jongboom 25:a8be9f3a530c 108 }
Jan Jongboom 25:a8be9f3a530c 109
Jan Jongboom 25:a8be9f3a530c 110 wait(osWaitForever);
Jan Jongboom 25:a8be9f3a530c 111 }
Jan Jongboom 25:a8be9f3a530c 112
Jan Jongboom 25:a8be9f3a530c 113 #endif