k jm / Mbed OS thingspark example

Dependencies:   MbedJSONValue mbed-http HTS221

Branch:
mbed-os-5.10
Revision:
31:66704f6f17c5
Parent:
30:4825e4f38844
diff -r 4825e4f38844 -r 66704f6f17c5 source/main-https-chunked-request.cpp
--- a/source/main-https-chunked-request.cpp	Mon Oct 29 14:34:43 2018 +0800
+++ b/source/main-https-chunked-request.cpp	Tue Oct 30 11:07:10 2018 +0800
@@ -8,6 +8,7 @@
 #if DEMO == DEMO_HTTPS_CHUNKED_REQUEST
 
 #include "mbed.h"
+#include "mbed_trace.h"
 #include "https_request.h"
 #include "network-helper.h"
 
@@ -34,13 +35,13 @@
     "-----END CERTIFICATE-----\n";
 
 void dump_response(HttpResponse* res) {
-    mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+    printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
 
-    mbedtls_printf("Headers:\n");
+    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());
+        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());
+    printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
 }
 
 // Spread the message out over 3 different chunks
@@ -53,7 +54,7 @@
 int chunk_ix = 0;
 
 // Callback function, grab the next chunk and return it
-const void * get_chunk(size_t* out_size) {
+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;
@@ -73,11 +74,16 @@
         return 1;
     }
 
-    // POST request to httpbin.org
+    mbed_trace_init();
+
+    // This example also logs the raw request, you can do this by calling 'set_request_log_buffer' on the request
+    uint8_t *request_buffer = (uint8_t*)calloc(2048, 1);
+
+    // POST request to reqres.in
     {
         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");
+        post_req->set_request_log_buffer(request_buffer, 2048);
 
         // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked
         HttpResponse* post_res = post_req->send(&get_chunk);
@@ -86,6 +92,15 @@
             return 1;
         }
 
+        // Log the raw request that went over the line (if you decode the hex you can see the chunked parts)
+        // e.g. in Node.js (take the output from below):
+        // '50 4f 53 54 20'.split(' ').map(c=>parseInt(c,16)).map(c=>String.fromCharCode(c)).join('')
+        printf("\n----- Request buffer -----\n");
+        for (size_t ix = 0; ix < post_req->get_request_log_buffer_length(); ix++) {
+            printf("%02x ", request_buffer[ix]);
+        }
+        printf("\n");
+
         printf("\n----- HTTPS POST response -----\n");
         dump_response(post_res);