this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Thu Feb 28 18:13:48 2019 +0000
Revision:
73:6f5021cbe752
commit current application code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 73:6f5021cbe752 1 #include "select-demo.h"
ocomeni 73:6f5021cbe752 2
ocomeni 73:6f5021cbe752 3 #if DEMO == DEMO_HTTP_IPV6
ocomeni 73:6f5021cbe752 4
ocomeni 73:6f5021cbe752 5 #include "mbed.h"
ocomeni 73:6f5021cbe752 6 #include "network-helper.h"
ocomeni 73:6f5021cbe752 7 #include "http_request.h"
ocomeni 73:6f5021cbe752 8
ocomeni 73:6f5021cbe752 9 void dump_response(HttpResponse* res) {
ocomeni 73:6f5021cbe752 10 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
ocomeni 73:6f5021cbe752 11
ocomeni 73:6f5021cbe752 12 printf("Headers:\n");
ocomeni 73:6f5021cbe752 13 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
ocomeni 73:6f5021cbe752 14 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
ocomeni 73:6f5021cbe752 15 }
ocomeni 73:6f5021cbe752 16 printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
ocomeni 73:6f5021cbe752 17 }
ocomeni 73:6f5021cbe752 18
ocomeni 73:6f5021cbe752 19 int main() {
ocomeni 73:6f5021cbe752 20 NetworkInterface* network = connect_to_default_network_interface();
ocomeni 73:6f5021cbe752 21 if (!network) {
ocomeni 73:6f5021cbe752 22 printf("Cannot connect to the network, see serial output\n");
ocomeni 73:6f5021cbe752 23 return 1;
ocomeni 73:6f5021cbe752 24 }
ocomeni 73:6f5021cbe752 25
ocomeni 73:6f5021cbe752 26 // Do a GET request to icanhazip.com which returns the public IPv6 address for the device
ocomeni 73:6f5021cbe752 27 // This page is only accessible over IPv6
ocomeni 73:6f5021cbe752 28 {
ocomeni 73:6f5021cbe752 29 // By default the body is automatically parsed and stored in a buffer, this is memory heavy.
ocomeni 73:6f5021cbe752 30 // To receive chunked response, pass in a callback as last parameter to the constructor.
ocomeni 73:6f5021cbe752 31 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://ipv6.icanhazip.com");
ocomeni 73:6f5021cbe752 32
ocomeni 73:6f5021cbe752 33 HttpResponse* get_res = get_req->send();
ocomeni 73:6f5021cbe752 34 if (!get_res) {
ocomeni 73:6f5021cbe752 35 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
ocomeni 73:6f5021cbe752 36 return 1;
ocomeni 73:6f5021cbe752 37 }
ocomeni 73:6f5021cbe752 38
ocomeni 73:6f5021cbe752 39 printf("\n----- HTTP GET response -----\n");
ocomeni 73:6f5021cbe752 40 dump_response(get_res);
ocomeni 73:6f5021cbe752 41
ocomeni 73:6f5021cbe752 42 delete get_req;
ocomeni 73:6f5021cbe752 43 }
ocomeni 73:6f5021cbe752 44
ocomeni 73:6f5021cbe752 45 wait(osWaitForever);
ocomeni 73:6f5021cbe752 46 }
ocomeni 73:6f5021cbe752 47
ocomeni 73:6f5021cbe752 48 #endif