simple http request (temperature data) from mbed lpc1768 with application board to thingsboard device/dashboard

Dependencies:   LM75B mbed-http

Fork of http-example by sandbox

Committer:
Jan Jongboom
Date:
Thu Jan 11 14:39:11 2018 +0100
Revision:
25:a8be9f3a530c
Child:
29:5ad8f931e4ff
Update mbed-http, add chunked requests

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 25:a8be9f3a530c 11 #include "https_request.h"
Jan Jongboom 25:a8be9f3a530c 12 #include "easy-connect.h"
Jan Jongboom 25:a8be9f3a530c 13
Jan Jongboom 25:a8be9f3a530c 14 Serial pc(USBTX, USBRX);
Jan Jongboom 25:a8be9f3a530c 15
Jan Jongboom 25:a8be9f3a530c 16 /* List of trusted root CA certificates
Jan Jongboom 25:a8be9f3a530c 17 * currently one: Comodo, the CA for reqres.in
Jan Jongboom 25:a8be9f3a530c 18 *
Jan Jongboom 25:a8be9f3a530c 19 * To add more root certificates, just concatenate them.
Jan Jongboom 25:a8be9f3a530c 20 */
Jan Jongboom 25:a8be9f3a530c 21 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
Jan Jongboom 25:a8be9f3a530c 22 "MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n"
Jan Jongboom 25:a8be9f3a530c 23 "MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n"
Jan Jongboom 25:a8be9f3a530c 24 "BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n"
Jan Jongboom 25:a8be9f3a530c 25 "IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n"
Jan Jongboom 25:a8be9f3a530c 26 "MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n"
Jan Jongboom 25:a8be9f3a530c 27 "ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n"
Jan Jongboom 25:a8be9f3a530c 28 "T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n"
Jan Jongboom 25:a8be9f3a530c 29 "biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n"
Jan Jongboom 25:a8be9f3a530c 30 "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n"
Jan Jongboom 25:a8be9f3a530c 31 "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n"
Jan Jongboom 25:a8be9f3a530c 32 "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n"
Jan Jongboom 25:a8be9f3a530c 33 "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n"
Jan Jongboom 25:a8be9f3a530c 34 "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n"
Jan Jongboom 25:a8be9f3a530c 35 "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n"
Jan Jongboom 25:a8be9f3a530c 36 "-----END CERTIFICATE-----\n";
Jan Jongboom 25:a8be9f3a530c 37
Jan Jongboom 25:a8be9f3a530c 38 void dump_response(HttpResponse* res) {
Jan Jongboom 25:a8be9f3a530c 39 mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
Jan Jongboom 25:a8be9f3a530c 40
Jan Jongboom 25:a8be9f3a530c 41 mbedtls_printf("Headers:\n");
Jan Jongboom 25:a8be9f3a530c 42 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
Jan Jongboom 25:a8be9f3a530c 43 mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
Jan Jongboom 25:a8be9f3a530c 44 }
Jan Jongboom 25:a8be9f3a530c 45 mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
Jan Jongboom 25:a8be9f3a530c 46 }
Jan Jongboom 25:a8be9f3a530c 47
Jan Jongboom 25:a8be9f3a530c 48 // Spread the message out over 3 different chunks
Jan Jongboom 25:a8be9f3a530c 49 const char * chunks[] = {
Jan Jongboom 25:a8be9f3a530c 50 "{\"message\":",
Jan Jongboom 25:a8be9f3a530c 51 "\"this is an example",
Jan Jongboom 25:a8be9f3a530c 52 " of chunked encoding\"}"
Jan Jongboom 25:a8be9f3a530c 53 };
Jan Jongboom 25:a8be9f3a530c 54
Jan Jongboom 25:a8be9f3a530c 55 int chunk_ix = 0;
Jan Jongboom 25:a8be9f3a530c 56
Jan Jongboom 25:a8be9f3a530c 57 // Callback function, grab the next chunk and return it
Jan Jongboom 25:a8be9f3a530c 58 const void * get_chunk(size_t* out_size) {
Jan Jongboom 25:a8be9f3a530c 59 // If you don't have any data left, set out_size to 0 and return a null pointer
Jan Jongboom 25:a8be9f3a530c 60 if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) {
Jan Jongboom 25:a8be9f3a530c 61 *out_size = 0;
Jan Jongboom 25:a8be9f3a530c 62 return NULL;
Jan Jongboom 25:a8be9f3a530c 63 }
Jan Jongboom 25:a8be9f3a530c 64 const char *chunk = chunks[chunk_ix];
Jan Jongboom 25:a8be9f3a530c 65 *out_size = strlen(chunk);
Jan Jongboom 25:a8be9f3a530c 66 chunk_ix++;
Jan Jongboom 25:a8be9f3a530c 67
Jan Jongboom 25:a8be9f3a530c 68 return chunk;
Jan Jongboom 25:a8be9f3a530c 69 }
Jan Jongboom 25:a8be9f3a530c 70
Jan Jongboom 25:a8be9f3a530c 71 int main() {
Jan Jongboom 25:a8be9f3a530c 72 pc.baud(115200);
Jan Jongboom 25:a8be9f3a530c 73
Jan Jongboom 25:a8be9f3a530c 74 NetworkInterface* network = easy_connect(true);
Jan Jongboom 25:a8be9f3a530c 75 if (!network) {
Jan Jongboom 25:a8be9f3a530c 76 return 1;
Jan Jongboom 25:a8be9f3a530c 77 }
Jan Jongboom 25:a8be9f3a530c 78
Jan Jongboom 25:a8be9f3a530c 79 // POST request to httpbin.org
Jan Jongboom 25:a8be9f3a530c 80 {
Jan Jongboom 25:a8be9f3a530c 81 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users");
Jan Jongboom 25:a8be9f3a530c 82 post_req->set_debug(true);
Jan Jongboom 25:a8be9f3a530c 83 post_req->set_header("Content-Type", "application/json");
Jan Jongboom 25:a8be9f3a530c 84
Jan Jongboom 25:a8be9f3a530c 85 // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked
Jan Jongboom 25:a8be9f3a530c 86 HttpResponse* post_res = post_req->send(&get_chunk);
Jan Jongboom 25:a8be9f3a530c 87 if (!post_res) {
Jan Jongboom 25:a8be9f3a530c 88 printf("HttpsRequest failed (error code %d)\n", post_req->get_error());
Jan Jongboom 25:a8be9f3a530c 89 return 1;
Jan Jongboom 25:a8be9f3a530c 90 }
Jan Jongboom 25:a8be9f3a530c 91
Jan Jongboom 25:a8be9f3a530c 92 printf("\n----- HTTPS POST response -----\n");
Jan Jongboom 25:a8be9f3a530c 93 dump_response(post_res);
Jan Jongboom 25:a8be9f3a530c 94
Jan Jongboom 25:a8be9f3a530c 95 delete post_req;
Jan Jongboom 25:a8be9f3a530c 96 }
Jan Jongboom 25:a8be9f3a530c 97
Jan Jongboom 25:a8be9f3a530c 98 wait(osWaitForever);
Jan Jongboom 25:a8be9f3a530c 99 }
Jan Jongboom 25:a8be9f3a530c 100
Jan Jongboom 25:a8be9f3a530c 101 #endif