sandbox / Mbed OS http-example

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main-https-chunked-request.cpp Source File

main-https-chunked-request.cpp

00001 /**
00002  * This is an example of doing chunked requests, where you do not need to load the full request body
00003  * into memory. You do this by adding a callback to the `send` function of the HTTP/HTTPS request.
00004  */
00005 
00006 #include "select-demo.h"
00007 
00008 #if DEMO == DEMO_HTTPS_CHUNKED_REQUEST
00009 
00010 #include "mbed.h"
00011 #include "mbed_trace.h"
00012 #include "https_request.h"
00013 #include "network-helper.h"
00014 
00015 /* List of trusted root CA certificates
00016  * currently one: Comodo, the CA for reqres.in
00017  *
00018  * To add more root certificates, just concatenate them.
00019  */
00020 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
00021     "MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n"
00022     "MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n"
00023     "BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n"
00024     "IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n"
00025     "MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n"
00026     "ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n"
00027     "T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n"
00028     "biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n"
00029     "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n"
00030     "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n"
00031     "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n"
00032     "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n"
00033     "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n"
00034     "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n"
00035     "-----END CERTIFICATE-----\n";
00036 
00037 void dump_response(HttpResponse* res) {
00038     printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
00039 
00040     printf("Headers:\n");
00041     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
00042         printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
00043     }
00044     printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
00045 }
00046 
00047 // Spread the message out over 3 different chunks
00048 const char * chunks[] = {
00049     "{\"message\":",
00050     "\"this is an example",
00051     " of chunked encoding\"}"
00052 };
00053 
00054 int chunk_ix = 0;
00055 
00056 // Callback function, grab the next chunk and return it
00057 const void * get_chunk(uint32_t* out_size) {
00058     // If you don't have any data left, set out_size to 0 and return a null pointer
00059     if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) {
00060         *out_size = 0;
00061         return NULL;
00062     }
00063     const char *chunk = chunks[chunk_ix];
00064     *out_size = strlen(chunk);
00065     chunk_ix++;
00066 
00067     return chunk;
00068 }
00069 
00070 int main() {
00071     NetworkInterface* network = connect_to_default_network_interface();
00072     if (!network) {
00073         printf("Cannot connect to the network, see serial output\n");
00074         return 1;
00075     }
00076 
00077     mbed_trace_init();
00078 
00079     // This example also logs the raw request, you can do this by calling 'set_request_log_buffer' on the request
00080     uint8_t *request_buffer = (uint8_t*)calloc(2048, 1);
00081 
00082     // POST request to reqres.in
00083     {
00084         HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users");
00085         post_req->set_header("Content-Type", "application/json");
00086         post_req->set_request_log_buffer(request_buffer, 2048);
00087 
00088         // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked
00089         HttpResponse* post_res = post_req->send(&get_chunk);
00090         if (!post_res) {
00091             printf("HttpsRequest failed (error code %d)\n", post_req->get_error());
00092             return 1;
00093         }
00094 
00095         // Log the raw request that went over the line (if you decode the hex you can see the chunked parts)
00096         // e.g. in Node.js (take the output from below):
00097         // '50 4f 53 54 20'.split(' ').map(c=>parseInt(c,16)).map(c=>String.fromCharCode(c)).join('')
00098         printf("\n----- Request buffer -----\n");
00099         for (size_t ix = 0; ix < post_req->get_request_log_buffer_length(); ix++) {
00100             printf("%02x ", request_buffer[ix]);
00101         }
00102         printf("\n");
00103 
00104         printf("\n----- HTTPS POST response -----\n");
00105         dump_response(post_res);
00106 
00107         delete post_req;
00108     }
00109 
00110     wait(osWaitForever);
00111 }
00112 
00113 #endif