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
source/main-http.cpp@21:92bdf75ae72b, 2017-12-14 (annotated)
- Committer:
- nrithya
- Date:
- Thu Dec 14 00:34:03 2017 +0000
- Revision:
- 21:92bdf75ae72b
- Parent:
- 20:1c3499729772
easy connect wnc added to http request with debug mode enabled. traces collected for AT%CGEQOS, AT%MEAS, AT%PCONI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
0:85fdc69bc10c | 1 | #include "select-demo.h" |
Jan Jongboom |
0:85fdc69bc10c | 2 | |
Jan Jongboom |
0:85fdc69bc10c | 3 | #if DEMO == DEMO_HTTP |
Jan Jongboom |
0:85fdc69bc10c | 4 | |
Jan Jongboom |
0:85fdc69bc10c | 5 | #include "mbed.h" |
Jan Jongboom |
0:85fdc69bc10c | 6 | #include "easy-connect.h" |
Jan Jongboom |
0:85fdc69bc10c | 7 | #include "http_request.h" |
Jan Jongboom |
0:85fdc69bc10c | 8 | |
Jan Jongboom |
0:85fdc69bc10c | 9 | Serial pc(USBTX, USBRX); |
nrithya | 21:92bdf75ae72b | 10 | Timer t; |
Jan Jongboom |
0:85fdc69bc10c | 11 | void dump_response(HttpResponse* res) { |
Jan Jongboom |
0:85fdc69bc10c | 12 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 13 | |
Jan Jongboom |
0:85fdc69bc10c | 14 | printf("Headers:\n"); |
Jan Jongboom |
0:85fdc69bc10c | 15 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
Jan Jongboom |
7:5d32909f77de | 16 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 17 | } |
Jan Jongboom |
5:42f713b19337 | 18 | printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 19 | } |
Jan Jongboom |
0:85fdc69bc10c | 20 | |
nrithya | 21:92bdf75ae72b | 21 | void body_callback(const char* data, size_t data_len) { |
nrithya | 21:92bdf75ae72b | 22 | // simply return, leading to the data being lost. |
nrithya | 21:92bdf75ae72b | 23 | // Necessary for large sizes in order to ensure that there isn't an |
nrithya | 21:92bdf75ae72b | 24 | // exhaustion of memory with the large video test. |
nrithya | 21:92bdf75ae72b | 25 | return; |
nrithya | 21:92bdf75ae72b | 26 | } |
nrithya | 21:92bdf75ae72b | 27 | |
Jan Jongboom |
0:85fdc69bc10c | 28 | int main() { |
Jan Jongboom |
0:85fdc69bc10c | 29 | pc.baud(115200); |
Jan Jongboom |
0:85fdc69bc10c | 30 | // Connect to the network (see mbed_app.json for the connectivity method used) |
nrithya | 21:92bdf75ae72b | 31 | for(int i=1;i<10;i++) { |
tdMBED | 20:1c3499729772 | 32 | printf("Hello?\n"); |
nrithya | 21:92bdf75ae72b | 33 | } |
Jan Jongboom |
0:85fdc69bc10c | 34 | NetworkInterface *network = easy_connect(true); |
Jan Jongboom |
0:85fdc69bc10c | 35 | if (!network) { |
Jan Jongboom |
0:85fdc69bc10c | 36 | printf("Cannot connect to the network, see serial output"); |
Jan Jongboom |
0:85fdc69bc10c | 37 | return 1; |
Jan Jongboom |
0:85fdc69bc10c | 38 | } |
Jan Jongboom |
0:85fdc69bc10c | 39 | |
tdMBED | 20:1c3499729772 | 40 | printf("See if it gives any debug info"); |
tdMBED | 20:1c3499729772 | 41 | |
Jan Jongboom |
0:85fdc69bc10c | 42 | // Do a GET request to httpbin.org |
nrithya | 21:92bdf75ae72b | 43 | //while(1) |
Jan Jongboom |
5:42f713b19337 | 44 | { |
Jan Jongboom |
14:3c173847e681 | 45 | // By default the body is automatically parsed and stored in a buffer, this is memory heavy. |
Jan Jongboom |
14:3c173847e681 | 46 | // To receive chunked response, pass in a callback as last parameter to the constructor. |
nrithya | 21:92bdf75ae72b | 47 | t.start(); |
nrithya | 21:92bdf75ae72b | 48 | // 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"); |
Jan Jongboom |
5:42f713b19337 | 49 | HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); |
Jan Jongboom |
5:42f713b19337 | 50 | HttpResponse* get_res = get_req->send(); |
nrithya | 21:92bdf75ae72b | 51 | t.stop(); |
nrithya | 21:92bdf75ae72b | 52 | printf("loading time: %f \n",t.read()); |
nrithya | 21:92bdf75ae72b | 53 | printf("Body_length: %d \n", get_res->get_body_length()); |
nrithya | 21:92bdf75ae72b | 54 | |
Jan Jongboom |
5:42f713b19337 | 55 | if (!get_res) { |
Jan Jongboom |
5:42f713b19337 | 56 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
Jan Jongboom |
5:42f713b19337 | 57 | return 1; |
Jan Jongboom |
5:42f713b19337 | 58 | } |
Jan Jongboom |
5:42f713b19337 | 59 | |
Jan Jongboom |
5:42f713b19337 | 60 | printf("\n----- HTTP GET response -----\n"); |
Jan Jongboom |
5:42f713b19337 | 61 | dump_response(get_res); |
nrithya | 21:92bdf75ae72b | 62 | t.reset(); |
Jan Jongboom |
5:42f713b19337 | 63 | |
Jan Jongboom |
5:42f713b19337 | 64 | delete get_req; |
Jan Jongboom |
0:85fdc69bc10c | 65 | } |
Jan Jongboom |
0:85fdc69bc10c | 66 | |
Jan Jongboom |
0:85fdc69bc10c | 67 | // POST request to httpbin.org |
nrithya | 21:92bdf75ae72b | 68 | //{ |
nrithya | 21:92bdf75ae72b | 69 | // HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); |
nrithya | 21:92bdf75ae72b | 70 | // post_req->set_header("Content-Type", "application/json"); |
nrithya | 21:92bdf75ae72b | 71 | // |
nrithya | 21:92bdf75ae72b | 72 | // const char body[] = "{\"hello\":\"world\"}"; |
nrithya | 21:92bdf75ae72b | 73 | // |
nrithya | 21:92bdf75ae72b | 74 | // HttpResponse* post_res = post_req->send(body, strlen(body)); |
nrithya | 21:92bdf75ae72b | 75 | // if (!post_res) { |
nrithya | 21:92bdf75ae72b | 76 | // printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
nrithya | 21:92bdf75ae72b | 77 | // return 1; |
nrithya | 21:92bdf75ae72b | 78 | // } |
nrithya | 21:92bdf75ae72b | 79 | // |
nrithya | 21:92bdf75ae72b | 80 | // printf("\n----- HTTP POST response -----\n"); |
nrithya | 21:92bdf75ae72b | 81 | // dump_response(post_res); |
nrithya | 21:92bdf75ae72b | 82 | // |
nrithya | 21:92bdf75ae72b | 83 | // delete post_req; |
nrithya | 21:92bdf75ae72b | 84 | // } |
Jan Jongboom |
0:85fdc69bc10c | 85 | |
Jan Jongboom |
0:85fdc69bc10c | 86 | Thread::wait(osWaitForever); |
Jan Jongboom |
0:85fdc69bc10c | 87 | } |
Jan Jongboom |
0:85fdc69bc10c | 88 | |
Jan Jongboom |
0:85fdc69bc10c | 89 | #endif |