Demonstration program with support for the WNC M14A2A Cellular LTE Data Module added. An additional demonstration program was also added that shows a few other features (for chunked responses).
Dependencies: easy-connect mbed-http
mbed-os-example-http(s) using WNC 14A2A Data Module
This application builds on the application provided by ARM (see https://developer.mbed.org/teams/sandbox/code/mbed-http/). It demonstrates how to make HTTP and HTTPS requests and parse the response from mbed OS 5.
There are a total of five demo's, which can be selected by modifying source/select-demo.h.
1. HTTP demo (DEMO_HTTP):
- Does a GET request to http://httpbin.org/status/418.
- Does a POST request to http://httpbin.org/post.
2. HTTPS demo (DEMO_HTTPS):
- Does a GET request to https://developer.mbed.org/media/uploads/mbed_official/hello.txt.
- Does a POST request to https://httpbin.org/post.
3. HTTP demo with socket re-use (DEMO_HTTP_SOCKET_REUSE).
- Similar to the HTTP demo but reuses the socket for all interactions
4. HTTPS demo with socket re-use (DEMO_HTTPS_SOCKET_REUSE).
- Similar to the HTTPS demo above
5. HTTP & HTTPS demo with socket re-use and chunked call-backs (DEMO_HTTPx)
- Performs the same calls via HTTP first and HTTPS second
- DOES A socket get from http://developer.mbed.org.
- Does a HTTP_GET from http://developer.mbed.org
- Does a HTTP_POST from http://httpbin.org/post
- Does a HTTP_PUT from http://httpbin.org/put
- Does a HTTP_DELETE from http://httpbin.org/delete
- Does a HTTP_GET from http://httpbin.org/stream
- Does a HTTP_GET from http://httpbin.org/get
Response parsing is done through [nodejs/http-parser](https://github.com/nodejs/http-parser).
To build
1. Open ``mbed_app.json`` and change the `network-interface` option to your connectivity method ([more info](https://github.com/ARMmbed/easy-connect)). 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.
Entropy (or lack thereof)
On all platforms except the FRDM-K64F and FRDM-K22F 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.
Tested on
- K64F with Ethernet.
- NUCLEO_F411RE with ESP8266.
- AT&T Cellular IoT Starter Kit with WNC M14A2A Cellular Data Module
The WNCInterface class currently supports the following version(s):
- MPSS: M14A2A_v11.50.164451 APSS: M14A2A_v11.53.164451
License
This library is released under the Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License and may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
source/main-x.cpp@18:2a891e583ec4, 2017-04-19 (annotated)
- Committer:
- JMF
- Date:
- Wed Apr 19 21:06:36 2017 +0000
- Revision:
- 18:2a891e583ec4
- Parent:
- 15:e96b74243a80
json file changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 15:e96b74243a80 | 1 | |
JMF | 15:e96b74243a80 | 2 | #include "select-demo.h" |
JMF | 15:e96b74243a80 | 3 | |
JMF | 15:e96b74243a80 | 4 | /* ===================================================================== |
JMF | 15:e96b74243a80 | 5 | Copyright © 2016, Avnet (R) |
JMF | 15:e96b74243a80 | 6 | |
JMF | 15:e96b74243a80 | 7 | Contributors: |
JMF | 15:e96b74243a80 | 8 | * James M Flynn, www.em.avnet.com |
JMF | 15:e96b74243a80 | 9 | |
JMF | 15:e96b74243a80 | 10 | Licensed under the Apache License, Version 2.0 (the "License"); |
JMF | 15:e96b74243a80 | 11 | you may not use this file except in compliance with the License. |
JMF | 15:e96b74243a80 | 12 | You may obtain a copy of the License at |
JMF | 15:e96b74243a80 | 13 | |
JMF | 15:e96b74243a80 | 14 | http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 15:e96b74243a80 | 15 | |
JMF | 15:e96b74243a80 | 16 | Unless required by applicable law or agreed to in writing, |
JMF | 15:e96b74243a80 | 17 | software distributed under the License is distributed on an |
JMF | 15:e96b74243a80 | 18 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
JMF | 15:e96b74243a80 | 19 | either express or implied. See the License for the specific |
JMF | 15:e96b74243a80 | 20 | language governing permissions and limitations under the License. |
JMF | 15:e96b74243a80 | 21 | |
JMF | 15:e96b74243a80 | 22 | @file main.cpp / WNC14A2AInterface_HTTP_exampel |
JMF | 15:e96b74243a80 | 23 | @version 1.0 |
JMF | 15:e96b74243a80 | 24 | @date Dec 2016 |
JMF | 15:e96b74243a80 | 25 | |
JMF | 15:e96b74243a80 | 26 | ======================================================================== */ |
JMF | 15:e96b74243a80 | 27 | |
JMF | 15:e96b74243a80 | 28 | #if DEMO == DEMO_HTTPx |
JMF | 15:e96b74243a80 | 29 | |
JMF | 15:e96b74243a80 | 30 | #include "mbed.h" |
JMF | 15:e96b74243a80 | 31 | #include "easy-connect.h" |
JMF | 15:e96b74243a80 | 32 | #include "http_request.h" |
JMF | 15:e96b74243a80 | 33 | #include "https_request.h" |
JMF | 15:e96b74243a80 | 34 | |
JMF | 15:e96b74243a80 | 35 | #define STREAM_CNT 10 //when we test streaming, this is how many times to stream the string |
JMF | 15:e96b74243a80 | 36 | #define STR_SIZE 150*(STREAM_CNT+1) //use a fixed size string buffer based on the streaming data count |
JMF | 15:e96b74243a80 | 37 | |
JMF | 15:e96b74243a80 | 38 | #define TOSTR(x) #x |
JMF | 15:e96b74243a80 | 39 | #define INTSTR(x) TOSTR(x) |
JMF | 15:e96b74243a80 | 40 | |
JMF | 15:e96b74243a80 | 41 | // |
JMF | 15:e96b74243a80 | 42 | // We do the same thing for both http and https, so create a macro to eaze the typing... |
JMF | 15:e96b74243a80 | 43 | // |
JMF | 15:e96b74243a80 | 44 | |
JMF | 15:e96b74243a80 | 45 | |
JMF | 15:e96b74243a80 | 46 | /* List of trusted root CA certificates |
JMF | 15:e96b74243a80 | 47 | * currently one: Let's Encrypt, the CA for httpbin.org |
JMF | 15:e96b74243a80 | 48 | * |
JMF | 15:e96b74243a80 | 49 | * To add more root certificates, just concatenate them. |
JMF | 15:e96b74243a80 | 50 | */ |
JMF | 15:e96b74243a80 | 51 | const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" |
JMF | 15:e96b74243a80 | 52 | "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" |
JMF | 15:e96b74243a80 | 53 | "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" |
JMF | 15:e96b74243a80 | 54 | "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" |
JMF | 15:e96b74243a80 | 55 | "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" |
JMF | 15:e96b74243a80 | 56 | "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" |
JMF | 15:e96b74243a80 | 57 | "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" |
JMF | 15:e96b74243a80 | 58 | "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" |
JMF | 15:e96b74243a80 | 59 | "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" |
JMF | 15:e96b74243a80 | 60 | "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" |
JMF | 15:e96b74243a80 | 61 | "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" |
JMF | 15:e96b74243a80 | 62 | "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" |
JMF | 15:e96b74243a80 | 63 | "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" |
JMF | 15:e96b74243a80 | 64 | "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" |
JMF | 15:e96b74243a80 | 65 | "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" |
JMF | 15:e96b74243a80 | 66 | "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" |
JMF | 15:e96b74243a80 | 67 | "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" |
JMF | 15:e96b74243a80 | 68 | "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" |
JMF | 15:e96b74243a80 | 69 | "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" |
JMF | 15:e96b74243a80 | 70 | "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" |
JMF | 15:e96b74243a80 | 71 | "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" |
JMF | 15:e96b74243a80 | 72 | "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" |
JMF | 15:e96b74243a80 | 73 | "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" |
JMF | 15:e96b74243a80 | 74 | "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" |
JMF | 15:e96b74243a80 | 75 | "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" |
JMF | 15:e96b74243a80 | 76 | "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" |
JMF | 15:e96b74243a80 | 77 | "-----END CERTIFICATE-----\n"; |
JMF | 15:e96b74243a80 | 78 | |
JMF | 15:e96b74243a80 | 79 | // |
JMF | 15:e96b74243a80 | 80 | // This example is setup to use MBED OS (5.2). It sets up a thread to call the different tests |
JMF | 15:e96b74243a80 | 81 | // so the default stack size can be varied. TLS is stack intensive and needs additional stack |
JMF | 15:e96b74243a80 | 82 | // to work effeciently. |
JMF | 15:e96b74243a80 | 83 | // |
JMF | 15:e96b74243a80 | 84 | |
JMF | 15:e96b74243a80 | 85 | Serial pc(USBTX, USBRX); |
JMF | 15:e96b74243a80 | 86 | |
JMF | 15:e96b74243a80 | 87 | // |
JMF | 15:e96b74243a80 | 88 | // The two test functions do the same set of tests, the first one uses standard HTTP methods while |
JMF | 15:e96b74243a80 | 89 | // the second test uses HTTPS. |
JMF | 15:e96b74243a80 | 90 | // |
JMF | 15:e96b74243a80 | 91 | |
JMF | 15:e96b74243a80 | 92 | void https_test_thread(void); //Thread that runs the two tests |
JMF | 15:e96b74243a80 | 93 | void test_http(NetworkInterface *net); //function makes standard HTTP calls |
JMF | 15:e96b74243a80 | 94 | void test_https(NetworkInterface *net); //function makes standard HTTPS calls |
JMF | 15:e96b74243a80 | 95 | |
JMF | 15:e96b74243a80 | 96 | int main() { |
JMF | 15:e96b74243a80 | 97 | Thread http_test(osPriorityNormal, DEFAULT_STACK_SIZE*4, NULL); |
JMF | 15:e96b74243a80 | 98 | pc.baud(115200); |
JMF | 15:e96b74243a80 | 99 | |
JMF | 15:e96b74243a80 | 100 | printf("Testing HTTP and HTTPS interface software.\n"); |
JMF | 15:e96b74243a80 | 101 | http_test.start(https_test_thread); |
JMF | 15:e96b74243a80 | 102 | while (true) { |
JMF | 15:e96b74243a80 | 103 | osDelay(500); |
JMF | 15:e96b74243a80 | 104 | } |
JMF | 15:e96b74243a80 | 105 | } |
JMF | 15:e96b74243a80 | 106 | |
JMF | 15:e96b74243a80 | 107 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 15:e96b74243a80 | 108 | // this is the Thread that calls each of the two tests. |
JMF | 15:e96b74243a80 | 109 | // |
JMF | 15:e96b74243a80 | 110 | void https_test_thread(void) { |
JMF | 15:e96b74243a80 | 111 | |
JMF | 15:e96b74243a80 | 112 | NetworkInterface *network = easy_connect(true); |
JMF | 15:e96b74243a80 | 113 | |
JMF | 15:e96b74243a80 | 114 | if (!network) { |
JMF | 15:e96b74243a80 | 115 | printf("Unable to connect to network!\n"); |
JMF | 15:e96b74243a80 | 116 | return; |
JMF | 15:e96b74243a80 | 117 | } |
JMF | 15:e96b74243a80 | 118 | else |
JMF | 15:e96b74243a80 | 119 | printf("My IP Address is: %s \n\n", network->get_ip_address()); |
JMF | 15:e96b74243a80 | 120 | |
JMF | 15:e96b74243a80 | 121 | test_http(network); |
JMF | 15:e96b74243a80 | 122 | test_https(network); |
JMF | 15:e96b74243a80 | 123 | |
JMF | 15:e96b74243a80 | 124 | printf(" - - - - - - - ALL DONE - - - - - - - \n"); |
JMF | 15:e96b74243a80 | 125 | |
JMF | 15:e96b74243a80 | 126 | network->disconnect(); |
JMF | 15:e96b74243a80 | 127 | delete network; |
JMF | 15:e96b74243a80 | 128 | Thread::wait(osWaitForever); |
JMF | 15:e96b74243a80 | 129 | } |
JMF | 15:e96b74243a80 | 130 | |
JMF | 15:e96b74243a80 | 131 | |
JMF | 15:e96b74243a80 | 132 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 15:e96b74243a80 | 133 | // Utility function to print out responses |
JMF | 15:e96b74243a80 | 134 | // |
JMF | 15:e96b74243a80 | 135 | void dump_response(HttpResponse* res) |
JMF | 15:e96b74243a80 | 136 | { |
JMF | 15:e96b74243a80 | 137 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
JMF | 15:e96b74243a80 | 138 | |
JMF | 15:e96b74243a80 | 139 | printf("Headers:\n"); |
JMF | 15:e96b74243a80 | 140 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
JMF | 15:e96b74243a80 | 141 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
JMF | 15:e96b74243a80 | 142 | } |
JMF | 15:e96b74243a80 | 143 | printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
JMF | 15:e96b74243a80 | 144 | } |
JMF | 15:e96b74243a80 | 145 | |
JMF | 15:e96b74243a80 | 146 | void stream_callback(const char *data, size_t len) |
JMF | 15:e96b74243a80 | 147 | { |
JMF | 15:e96b74243a80 | 148 | printf("Chunk Received:\n"); |
JMF | 15:e96b74243a80 | 149 | for( size_t x=0; x<len; x++ ) |
JMF | 15:e96b74243a80 | 150 | printf("%c",data[x]); |
JMF | 15:e96b74243a80 | 151 | printf("\n"); |
JMF | 15:e96b74243a80 | 152 | } |
JMF | 15:e96b74243a80 | 153 | |
JMF | 15:e96b74243a80 | 154 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 15:e96b74243a80 | 155 | // test the HTTP client class |
JMF | 15:e96b74243a80 | 156 | // |
JMF | 15:e96b74243a80 | 157 | void test_http(NetworkInterface *net) |
JMF | 15:e96b74243a80 | 158 | { |
JMF | 15:e96b74243a80 | 159 | TCPSocket *socket = new TCPSocket(); |
JMF | 15:e96b74243a80 | 160 | |
JMF | 15:e96b74243a80 | 161 | printf(">>>>>>>>>>>><<<<<<<<<<<<\n"); |
JMF | 15:e96b74243a80 | 162 | printf(">>> TEST HTTPClient <<<\n"); |
JMF | 15:e96b74243a80 | 163 | printf(">>>>>>>>>>>><<<<<<<<<<<<\n\n"); |
JMF | 15:e96b74243a80 | 164 | |
JMF | 15:e96b74243a80 | 165 | nsapi_error_t open_result = socket->open(net); |
JMF | 15:e96b74243a80 | 166 | if (open_result != 0) { |
JMF | 15:e96b74243a80 | 167 | printf("Opening TCPSocket failed... %d\n", open_result); |
JMF | 15:e96b74243a80 | 168 | return; |
JMF | 15:e96b74243a80 | 169 | } |
JMF | 15:e96b74243a80 | 170 | |
JMF | 15:e96b74243a80 | 171 | nsapi_error_t connect_result = socket->connect("developer.mbed.org", 80); |
JMF | 15:e96b74243a80 | 172 | if (connect_result != 0) { |
JMF | 15:e96b74243a80 | 173 | printf("Connecting over TCPSocket failed... %d\n", connect_result); |
JMF | 15:e96b74243a80 | 174 | return; |
JMF | 15:e96b74243a80 | 175 | } |
JMF | 15:e96b74243a80 | 176 | else |
JMF | 15:e96b74243a80 | 177 | printf("Connected over TCP to developer.mbed.org\n\n"); |
JMF | 15:e96b74243a80 | 178 | |
JMF | 15:e96b74243a80 | 179 | printf(" >>>First, lets get a page from http://developer.mbed.org\n"); |
JMF | 15:e96b74243a80 | 180 | { |
JMF | 15:e96b74243a80 | 181 | HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"http://developer.mbed.org/media/uploads/mbed_official/hello.txt"); |
JMF | 15:e96b74243a80 | 182 | HttpResponse* get_res = get_req->send(); |
JMF | 15:e96b74243a80 | 183 | if (!get_res) { |
JMF | 15:e96b74243a80 | 184 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
JMF | 15:e96b74243a80 | 185 | return; |
JMF | 15:e96b74243a80 | 186 | } |
JMF | 15:e96b74243a80 | 187 | |
JMF | 15:e96b74243a80 | 188 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 189 | dump_response(get_res); |
JMF | 15:e96b74243a80 | 190 | delete get_req; |
JMF | 15:e96b74243a80 | 191 | } |
JMF | 15:e96b74243a80 | 192 | delete socket; |
JMF | 15:e96b74243a80 | 193 | |
JMF | 15:e96b74243a80 | 194 | socket = new TCPSocket(); |
JMF | 15:e96b74243a80 | 195 | |
JMF | 15:e96b74243a80 | 196 | open_result = socket->open(net); |
JMF | 15:e96b74243a80 | 197 | if (open_result != 0) { |
JMF | 15:e96b74243a80 | 198 | printf("Opening TCPSocket failed... %d\n", open_result); |
JMF | 15:e96b74243a80 | 199 | return; |
JMF | 15:e96b74243a80 | 200 | } |
JMF | 15:e96b74243a80 | 201 | |
JMF | 15:e96b74243a80 | 202 | connect_result = socket->connect("httpbin.org", 80); |
JMF | 15:e96b74243a80 | 203 | if (connect_result != 0) { |
JMF | 15:e96b74243a80 | 204 | printf("Connecting over TCPSocket http://httpbin.org... %d\n", connect_result); |
JMF | 15:e96b74243a80 | 205 | return; |
JMF | 15:e96b74243a80 | 206 | } |
JMF | 15:e96b74243a80 | 207 | else |
JMF | 15:e96b74243a80 | 208 | printf("Connected over TCP to httpbin.org\n\n"); |
JMF | 15:e96b74243a80 | 209 | |
JMF | 15:e96b74243a80 | 210 | printf("\n\n >>>Post data... **\n"); |
JMF | 15:e96b74243a80 | 211 | { |
JMF | 15:e96b74243a80 | 212 | HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post"); |
JMF | 15:e96b74243a80 | 213 | post_req->set_header("Content-Type", "application/json"); |
JMF | 15:e96b74243a80 | 214 | const char body[] = "{\"hello\":\"world\"}," |
JMF | 15:e96b74243a80 | 215 | "{\"test\":\"1234\"}"; |
JMF | 15:e96b74243a80 | 216 | |
JMF | 15:e96b74243a80 | 217 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
JMF | 15:e96b74243a80 | 218 | if (!post_res) { |
JMF | 15:e96b74243a80 | 219 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
JMF | 15:e96b74243a80 | 220 | return; |
JMF | 15:e96b74243a80 | 221 | } |
JMF | 15:e96b74243a80 | 222 | |
JMF | 15:e96b74243a80 | 223 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 224 | dump_response(post_res); |
JMF | 15:e96b74243a80 | 225 | delete post_req; |
JMF | 15:e96b74243a80 | 226 | } |
JMF | 15:e96b74243a80 | 227 | |
JMF | 15:e96b74243a80 | 228 | printf("\n\n >>>Put data... \n"); |
JMF | 15:e96b74243a80 | 229 | { |
JMF | 15:e96b74243a80 | 230 | HttpRequest* put_req = new HttpRequest(socket, HTTP_PUT, "http://httpbin.org/put"); |
JMF | 15:e96b74243a80 | 231 | put_req->set_header("Content-Type", "application/json"); |
JMF | 15:e96b74243a80 | 232 | |
JMF | 15:e96b74243a80 | 233 | const char body[] = "This is a PUT test!"; |
JMF | 15:e96b74243a80 | 234 | |
JMF | 15:e96b74243a80 | 235 | HttpResponse* put_res = put_req->send(body, strlen(body)); |
JMF | 15:e96b74243a80 | 236 | if (!put_res) { |
JMF | 15:e96b74243a80 | 237 | printf("HttpRequest failed (error code %d)\n", put_req->get_error()); |
JMF | 15:e96b74243a80 | 238 | return; |
JMF | 15:e96b74243a80 | 239 | } |
JMF | 15:e96b74243a80 | 240 | |
JMF | 15:e96b74243a80 | 241 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 242 | dump_response(put_res); |
JMF | 15:e96b74243a80 | 243 | delete put_req; |
JMF | 15:e96b74243a80 | 244 | } |
JMF | 15:e96b74243a80 | 245 | |
JMF | 15:e96b74243a80 | 246 | printf("\n\n >>>Delete data... \n"); |
JMF | 15:e96b74243a80 | 247 | { |
JMF | 15:e96b74243a80 | 248 | HttpRequest* del_req = new HttpRequest(socket, HTTP_DELETE, "http://httpbin.org/delete"); |
JMF | 15:e96b74243a80 | 249 | del_req->set_header("Content-Type", "application/json"); |
JMF | 15:e96b74243a80 | 250 | |
JMF | 15:e96b74243a80 | 251 | HttpResponse* del_res = del_req->send(); |
JMF | 15:e96b74243a80 | 252 | if (!del_res) { |
JMF | 15:e96b74243a80 | 253 | printf("HttpRequest failed (error code %d)\n", del_req->get_error()); |
JMF | 15:e96b74243a80 | 254 | return; |
JMF | 15:e96b74243a80 | 255 | } |
JMF | 15:e96b74243a80 | 256 | |
JMF | 15:e96b74243a80 | 257 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 258 | dump_response(del_res); |
JMF | 15:e96b74243a80 | 259 | delete del_req; |
JMF | 15:e96b74243a80 | 260 | } |
JMF | 15:e96b74243a80 | 261 | |
JMF | 15:e96b74243a80 | 262 | printf("\n\n >>>HTTP:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT) "... \n"); |
JMF | 15:e96b74243a80 | 263 | { |
JMF | 15:e96b74243a80 | 264 | HttpRequest* stream_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT), |
JMF | 15:e96b74243a80 | 265 | stream_callback ); |
JMF | 15:e96b74243a80 | 266 | stream_req->send(); |
JMF | 15:e96b74243a80 | 267 | delete stream_req; |
JMF | 15:e96b74243a80 | 268 | } |
JMF | 15:e96b74243a80 | 269 | |
JMF | 15:e96b74243a80 | 270 | printf("\n\n >>>HTTP:Status...\n"); |
JMF | 15:e96b74243a80 | 271 | { |
JMF | 15:e96b74243a80 | 272 | HttpRequest* get_req = new HttpRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1"); |
JMF | 15:e96b74243a80 | 273 | HttpResponse* get_res = get_req->send(); |
JMF | 15:e96b74243a80 | 274 | if (!get_res) { |
JMF | 15:e96b74243a80 | 275 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
JMF | 15:e96b74243a80 | 276 | return; |
JMF | 15:e96b74243a80 | 277 | } |
JMF | 15:e96b74243a80 | 278 | |
JMF | 15:e96b74243a80 | 279 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 280 | dump_response(get_res); |
JMF | 15:e96b74243a80 | 281 | delete get_req; |
JMF | 15:e96b74243a80 | 282 | } |
JMF | 15:e96b74243a80 | 283 | delete socket; |
JMF | 15:e96b74243a80 | 284 | } |
JMF | 15:e96b74243a80 | 285 | |
JMF | 15:e96b74243a80 | 286 | |
JMF | 15:e96b74243a80 | 287 | void dump_httpsresponse(HttpResponse* res) |
JMF | 15:e96b74243a80 | 288 | { |
JMF | 15:e96b74243a80 | 289 | mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
JMF | 15:e96b74243a80 | 290 | |
JMF | 15:e96b74243a80 | 291 | mbedtls_printf("Headers:\n"); |
JMF | 15:e96b74243a80 | 292 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
JMF | 15:e96b74243a80 | 293 | mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
JMF | 15:e96b74243a80 | 294 | } |
JMF | 15:e96b74243a80 | 295 | mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
JMF | 15:e96b74243a80 | 296 | } |
JMF | 15:e96b74243a80 | 297 | |
JMF | 15:e96b74243a80 | 298 | void stream_httpscallback(const char *data, size_t len) |
JMF | 15:e96b74243a80 | 299 | { |
JMF | 15:e96b74243a80 | 300 | printf("Chunk Received:\n"); |
JMF | 15:e96b74243a80 | 301 | for( size_t x=0; x<len; x++ ) |
JMF | 15:e96b74243a80 | 302 | printf("%c",data[x]); |
JMF | 15:e96b74243a80 | 303 | printf("\n"); |
JMF | 15:e96b74243a80 | 304 | } |
JMF | 15:e96b74243a80 | 305 | |
JMF | 15:e96b74243a80 | 306 | //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
JMF | 15:e96b74243a80 | 307 | // test the HTTPS client class |
JMF | 15:e96b74243a80 | 308 | // |
JMF | 15:e96b74243a80 | 309 | void test_https(NetworkInterface *net) |
JMF | 15:e96b74243a80 | 310 | { |
JMF | 15:e96b74243a80 | 311 | |
JMF | 15:e96b74243a80 | 312 | printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n"); |
JMF | 15:e96b74243a80 | 313 | printf(">>> TEST HTTPS - set up TLS connection <<<\n"); |
JMF | 15:e96b74243a80 | 314 | printf(">>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<\n\n"); |
JMF | 15:e96b74243a80 | 315 | |
JMF | 15:e96b74243a80 | 316 | TLSSocket* socket = new TLSSocket(net, "httpbin.org", 443, SSL_CA_PEM); |
JMF | 15:e96b74243a80 | 317 | socket->set_debug(true); |
JMF | 15:e96b74243a80 | 318 | if (socket->connect() != 0) { |
JMF | 15:e96b74243a80 | 319 | printf("TLS Connect failed %d\n", socket->error()); |
JMF | 15:e96b74243a80 | 320 | return; |
JMF | 15:e96b74243a80 | 321 | } |
JMF | 15:e96b74243a80 | 322 | |
JMF | 15:e96b74243a80 | 323 | printf("\n\n >>>Post data... **\n"); |
JMF | 15:e96b74243a80 | 324 | { |
JMF | 15:e96b74243a80 | 325 | HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "http://httpbin.org/post"); |
JMF | 15:e96b74243a80 | 326 | post_req->set_header("Content-Type", "application/json"); |
JMF | 15:e96b74243a80 | 327 | const char body[] = "{\"hello\":\"world\"}," |
JMF | 15:e96b74243a80 | 328 | "{\"test\":\"1234\"}"; |
JMF | 15:e96b74243a80 | 329 | |
JMF | 15:e96b74243a80 | 330 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
JMF | 15:e96b74243a80 | 331 | if (!post_res) { |
JMF | 15:e96b74243a80 | 332 | printf("HttpsRequest failed (error code %d)\n", post_req->get_error()); |
JMF | 15:e96b74243a80 | 333 | return; |
JMF | 15:e96b74243a80 | 334 | } |
JMF | 15:e96b74243a80 | 335 | |
JMF | 15:e96b74243a80 | 336 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 337 | dump_httpsresponse(post_res); |
JMF | 15:e96b74243a80 | 338 | delete post_req; |
JMF | 15:e96b74243a80 | 339 | } |
JMF | 15:e96b74243a80 | 340 | |
JMF | 15:e96b74243a80 | 341 | printf("\n\n >>>Put data... \n"); |
JMF | 15:e96b74243a80 | 342 | { |
JMF | 15:e96b74243a80 | 343 | HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, "http://httpbin.org/put"); |
JMF | 15:e96b74243a80 | 344 | put_req->set_header("Content-Type", "application/json"); |
JMF | 15:e96b74243a80 | 345 | |
JMF | 15:e96b74243a80 | 346 | const char body[] = "This is a PUT test!"; |
JMF | 15:e96b74243a80 | 347 | |
JMF | 15:e96b74243a80 | 348 | HttpResponse* put_res = put_req->send(body, strlen(body)); |
JMF | 15:e96b74243a80 | 349 | if (!put_res) { |
JMF | 15:e96b74243a80 | 350 | printf("HttpsRequest failed (error code %d)\n", put_req->get_error()); |
JMF | 15:e96b74243a80 | 351 | return; |
JMF | 15:e96b74243a80 | 352 | } |
JMF | 15:e96b74243a80 | 353 | |
JMF | 15:e96b74243a80 | 354 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 355 | dump_httpsresponse(put_res); |
JMF | 15:e96b74243a80 | 356 | delete put_req; |
JMF | 15:e96b74243a80 | 357 | } |
JMF | 15:e96b74243a80 | 358 | |
JMF | 15:e96b74243a80 | 359 | printf("\n\n >>>Delete data... \n"); |
JMF | 15:e96b74243a80 | 360 | { |
JMF | 15:e96b74243a80 | 361 | HttpsRequest* del_req = new HttpsRequest(socket, HTTP_DELETE, "http://httpbin.org/delete"); |
JMF | 15:e96b74243a80 | 362 | del_req->set_header("Content-Type", "application/json"); |
JMF | 15:e96b74243a80 | 363 | |
JMF | 15:e96b74243a80 | 364 | HttpResponse* del_res = del_req->send(); |
JMF | 15:e96b74243a80 | 365 | if (!del_res) { |
JMF | 15:e96b74243a80 | 366 | printf("HttpsRequest failed (error code %d)\n", del_req->get_error()); |
JMF | 15:e96b74243a80 | 367 | return; |
JMF | 15:e96b74243a80 | 368 | } |
JMF | 15:e96b74243a80 | 369 | |
JMF | 15:e96b74243a80 | 370 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 371 | dump_httpsresponse(del_res); |
JMF | 15:e96b74243a80 | 372 | delete del_req; |
JMF | 15:e96b74243a80 | 373 | } |
JMF | 15:e96b74243a80 | 374 | |
JMF | 15:e96b74243a80 | 375 | printf("\n\n >>>HTTP:stream, send http://httpbin.org/stream/" INTSTR(STREAM_CNT)"... \n"); |
JMF | 15:e96b74243a80 | 376 | { |
JMF | 15:e96b74243a80 | 377 | HttpsRequest* stream_req = new HttpsRequest(socket, HTTP_GET, "http://httpbin.org/stream/" INTSTR(STREAM_CNT), |
JMF | 15:e96b74243a80 | 378 | stream_httpscallback ); |
JMF | 15:e96b74243a80 | 379 | stream_req->send(); |
JMF | 15:e96b74243a80 | 380 | delete stream_req; |
JMF | 15:e96b74243a80 | 381 | } |
JMF | 15:e96b74243a80 | 382 | |
JMF | 15:e96b74243a80 | 383 | printf("\n\n >>>HTTP:Status...\n"); |
JMF | 15:e96b74243a80 | 384 | { |
JMF | 15:e96b74243a80 | 385 | HttpsRequest* get_req = new HttpsRequest(socket,HTTP_GET,"http://httpbin.org/get?show_env=1"); |
JMF | 15:e96b74243a80 | 386 | HttpResponse* get_res = get_req->send(); |
JMF | 15:e96b74243a80 | 387 | if (!get_res) { |
JMF | 15:e96b74243a80 | 388 | printf("HttpsRequest failed (error code %d)\n", get_req->get_error()); |
JMF | 15:e96b74243a80 | 389 | return; |
JMF | 15:e96b74243a80 | 390 | } |
JMF | 15:e96b74243a80 | 391 | |
JMF | 15:e96b74243a80 | 392 | printf("\n----- RESPONSE: -----\n"); |
JMF | 15:e96b74243a80 | 393 | dump_httpsresponse(get_res); |
JMF | 15:e96b74243a80 | 394 | delete get_req; |
JMF | 15:e96b74243a80 | 395 | } |
JMF | 15:e96b74243a80 | 396 | delete socket; |
JMF | 15:e96b74243a80 | 397 | } |
JMF | 15:e96b74243a80 | 398 | |
JMF | 15:e96b74243a80 | 399 | #endif // DEMO |
JMF | 15:e96b74243a80 | 400 |