easy connect wnc added to http request with debug mode enabled. traces collected for AT%CGEQOS, AT%MEAS, AT%PCONI

Dependencies:   easy-connect-httpmodified mbed-http

Fork of http-example-wnc-modified by Tyler Davis

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main-http.cpp Source File

main-http.cpp

00001 #include "select-demo.h"
00002 
00003 #if DEMO == DEMO_HTTP
00004 
00005 #include "mbed.h"
00006 #include "easy-connect.h"
00007 #include "http_request.h"
00008 
00009 Serial pc(USBTX, USBRX);
00010 Timer t;
00011 void dump_response(HttpResponse* res) {
00012     printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
00013 
00014     printf("Headers:\n");
00015     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
00016         printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
00017     }
00018     printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
00019 }
00020 
00021 void body_callback(const char* data, size_t data_len) {
00022     // simply return, leading to the data being lost.
00023     // Necessary for large sizes in order to ensure that there isn't an 
00024     // exhaustion of memory with the large video test.
00025     return;
00026 }
00027 
00028 int main() {
00029     pc.baud(115200);
00030     // Connect to the network (see mbed_app.json for the connectivity method used)
00031     for(int i=1;i<10;i++) {
00032         printf("Hello?\n");
00033 }
00034     NetworkInterface *network = easy_connect(true);
00035     if (!network) {
00036         printf("Cannot connect to the network, see serial output");
00037         return 1;
00038     }
00039 
00040     printf("See if it gives any debug info");
00041 
00042     // Do a GET request to httpbin.org
00043     //while(1)
00044     {
00045         // By default the body is automatically parsed and stored in a buffer, this is memory heavy.
00046         // To receive chunked response, pass in a callback as last parameter to the constructor.
00047         t.start();
00048         // HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "https://www.google.com/search?q=%E2%80%9CTrading+Structure+for+Randomness+in+Wireless+Opportunistic+Routing%E2%80%9D&oq=%E2%80%9CTrading+Structure+for+Randomness+in+Wireless+Opportunistic+Routing%E2%80%9D&aqs=chrome..69i57j0j69i60.1298j0j8&sourceid=chrome&ie=UTF-8");
00049         HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418");
00050         HttpResponse* get_res = get_req->send();
00051         t.stop();
00052         printf("loading time: %f \n",t.read());
00053         printf("Body_length: %d \n", get_res->get_body_length());
00054         
00055         if (!get_res) {
00056             printf("HttpRequest failed (error code %d)\n", get_req->get_error());
00057             return 1;
00058         }
00059 
00060         printf("\n----- HTTP GET response -----\n");
00061         dump_response(get_res);
00062         t.reset();
00063 
00064         delete get_req;
00065     }
00066 
00067     // POST request to httpbin.org
00068     //{
00069 //        HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
00070 //        post_req->set_header("Content-Type", "application/json");
00071 //
00072 //        const char body[] = "{\"hello\":\"world\"}";
00073 //
00074 //        HttpResponse* post_res = post_req->send(body, strlen(body));
00075 //        if (!post_res) {
00076 //            printf("HttpRequest failed (error code %d)\n", post_req->get_error());
00077 //            return 1;
00078 //        }
00079 //
00080 //        printf("\n----- HTTP POST response -----\n");
00081 //        dump_response(post_res);
00082 //
00083 //        delete post_req;
00084 //    }
00085 
00086     Thread::wait(osWaitForever);
00087 }
00088 
00089 #endif