HTTP and HTTPS example application for Mbed OS 5
Dependencies: mbed-http
main-http-ipv6.cpp
00001 #include "select-demo.h" 00002 00003 #if DEMO == DEMO_HTTP_IPV6 00004 00005 #include "mbed.h" 00006 #include "network-helper.h" 00007 #include "http_request.h" 00008 00009 void dump_response(HttpResponse* res) { 00010 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); 00011 00012 printf("Headers:\n"); 00013 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00014 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); 00015 } 00016 printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); 00017 } 00018 00019 int main() { 00020 NetworkInterface* network = connect_to_default_network_interface(); 00021 if (!network) { 00022 printf("Cannot connect to the network, see serial output\n"); 00023 return 1; 00024 } 00025 00026 // Do a GET request to icanhazip.com which returns the public IPv6 address for the device 00027 // This page is only accessible over IPv6 00028 { 00029 // By default the body is automatically parsed and stored in a buffer, this is memory heavy. 00030 // To receive chunked response, pass in a callback as last parameter to the constructor. 00031 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://ipv6.icanhazip.com"); 00032 00033 HttpResponse* get_res = get_req->send(); 00034 if (!get_res) { 00035 printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00036 return 1; 00037 } 00038 00039 printf("\n----- HTTP GET response -----\n"); 00040 dump_response(get_res); 00041 00042 delete get_req; 00043 } 00044 00045 wait(osWaitForever); 00046 } 00047 00048 #endif
Generated on Thu Jul 14 2022 06:47:14 by 1.7.2