Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of http-example by
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 "https_request.h" 00012 #include "easy-connect.h" 00013 00014 /* List of trusted root CA certificates 00015 * currently one: Comodo, the CA for reqres.in 00016 * 00017 * To add more root certificates, just concatenate them. 00018 */ 00019 const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" 00020 "MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n" 00021 "MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n" 00022 "BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n" 00023 "IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n" 00024 "MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n" 00025 "ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n" 00026 "T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n" 00027 "biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n" 00028 "FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n" 00029 "cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n" 00030 "BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n" 00031 "BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n" 00032 "fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n" 00033 "GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n" 00034 "-----END CERTIFICATE-----\n"; 00035 00036 void dump_response(HttpResponse* res) { 00037 mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); 00038 00039 mbedtls_printf("Headers:\n"); 00040 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00041 mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); 00042 } 00043 mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); 00044 } 00045 00046 // Spread the message out over 3 different chunks 00047 const char * chunks[] = { 00048 "{\"message\":", 00049 "\"this is an example", 00050 " of chunked encoding\"}" 00051 }; 00052 00053 int chunk_ix = 0; 00054 00055 // Callback function, grab the next chunk and return it 00056 const void * get_chunk(size_t* out_size) { 00057 // If you don't have any data left, set out_size to 0 and return a null pointer 00058 if (chunk_ix == (sizeof(chunks) / sizeof(chunks[0]))) { 00059 *out_size = 0; 00060 return NULL; 00061 } 00062 const char *chunk = chunks[chunk_ix]; 00063 *out_size = strlen(chunk); 00064 chunk_ix++; 00065 00066 return chunk; 00067 } 00068 00069 int main() { 00070 NetworkInterface* network = easy_connect(true); 00071 if (!network) { 00072 return 1; 00073 } 00074 00075 // POST request to httpbin.org 00076 { 00077 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://reqres.in/api/users"); 00078 post_req->set_debug(true); 00079 post_req->set_header("Content-Type", "application/json"); 00080 00081 // If you pass a callback here, the Transfer-Encoding header is automatically set to chunked 00082 HttpResponse* post_res = post_req->send(&get_chunk); 00083 if (!post_res) { 00084 printf("HttpsRequest failed (error code %d)\n", post_req->get_error()); 00085 return 1; 00086 } 00087 00088 printf("\n----- HTTPS POST response -----\n"); 00089 dump_response(post_res); 00090 00091 delete post_req; 00092 } 00093 00094 wait(osWaitForever); 00095 } 00096 00097 #endif
Generated on Wed Jul 13 2022 20:30:07 by
1.7.2
