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
main-http-socket-reuse.cpp
00001 #include "select-demo.h" 00002 00003 #if DEMO == DEMO_HTTP_SOCKET_REUSE 00004 00005 #include "mbed.h" 00006 #include "easy-connect.h" 00007 #include "http_request.h" 00008 00009 Serial pc(USBTX, USBRX); 00010 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 int main() { 00022 pc.baud(115200); 00023 // Connect to the network (see mbed_app.json for the connectivity method used) 00024 NetworkInterface *network = easy_connect(true); 00025 if (!network) { 00026 printf("Cannot connect to the network, see serial output"); 00027 return 1; 00028 } 00029 00030 // Create a TCP socket 00031 printf("\n----- Setting up TCP connection -----\n"); 00032 00033 TCPSocket* socket = new TCPSocket(); 00034 nsapi_error_t open_result = socket->open(network); 00035 if (open_result != 0) { 00036 printf("Opening TCPSocket failed... %d\n", open_result); 00037 return 1; 00038 } 00039 00040 nsapi_error_t connect_result = socket->connect("httpbin.org", 80); 00041 if (connect_result != 0) { 00042 printf("Connecting over TCPSocket failed... %d\n", connect_result); 00043 return 1; 00044 } 00045 00046 printf("Connected over TCP to httpbin.org:80\n"); 00047 00048 // Do a GET request to httpbin.org 00049 { 00050 HttpRequest* get_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418"); 00051 00052 // By default the body is automatically parsed and stored in a string, this is memory heavy. 00053 // To receive chunked response, pass in a callback as third parameter to 'send'. 00054 HttpResponse* get_res = get_req->send(); 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 00063 delete get_req; 00064 } 00065 00066 // POST request to httpbin.org 00067 { 00068 HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post"); 00069 post_req->set_header("Content-Type", "application/json"); 00070 00071 const char body[] = "{\"hello\":\"world\"}"; 00072 00073 HttpResponse* post_res = post_req->send(body, strlen(body)); 00074 if (!post_res) { 00075 printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00076 return 1; 00077 } 00078 00079 printf("\n----- HTTP POST response -----\n"); 00080 dump_response(post_res); 00081 00082 delete post_req; 00083 } 00084 00085 delete socket; 00086 00087 Thread::wait(osWaitForever); 00088 } 00089 00090 #endif
Generated on Fri Jul 15 2022 07:04:11 by
1.7.2
