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_SOCKET_REUSE
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 // Create a TCP socket
ocomeni 73:6f5021cbe752 27 printf("\n----- Setting up TCP connection -----\n");
ocomeni 73:6f5021cbe752 28
ocomeni 73:6f5021cbe752 29 TCPSocket* socket = new TCPSocket();
ocomeni 73:6f5021cbe752 30 nsapi_error_t open_result = socket->open(network);
ocomeni 73:6f5021cbe752 31 if (open_result != 0) {
ocomeni 73:6f5021cbe752 32 printf("Opening TCPSocket failed... %d\n", open_result);
ocomeni 73:6f5021cbe752 33 return 1;
ocomeni 73:6f5021cbe752 34 }
ocomeni 73:6f5021cbe752 35
ocomeni 73:6f5021cbe752 36 nsapi_error_t connect_result = socket->connect("httpbin.org", 80);
ocomeni 73:6f5021cbe752 37 if (connect_result != 0) {
ocomeni 73:6f5021cbe752 38 printf("Connecting over TCPSocket failed... %d\n", connect_result);
ocomeni 73:6f5021cbe752 39 return 1;
ocomeni 73:6f5021cbe752 40 }
ocomeni 73:6f5021cbe752 41
ocomeni 73:6f5021cbe752 42 printf("Connected over TCP to httpbin.org:80\n");
ocomeni 73:6f5021cbe752 43
ocomeni 73:6f5021cbe752 44 // Do a GET request to httpbin.org
ocomeni 73:6f5021cbe752 45 {
ocomeni 73:6f5021cbe752 46 HttpRequest* get_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418");
ocomeni 73:6f5021cbe752 47
ocomeni 73:6f5021cbe752 48 // By default the body is automatically parsed and stored in a string, this is memory heavy.
ocomeni 73:6f5021cbe752 49 // To receive chunked response, pass in a callback as third parameter to 'send'.
ocomeni 73:6f5021cbe752 50 HttpResponse* get_res = get_req->send();
ocomeni 73:6f5021cbe752 51 if (!get_res) {
ocomeni 73:6f5021cbe752 52 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
ocomeni 73:6f5021cbe752 53 return 1;
ocomeni 73:6f5021cbe752 54 }
ocomeni 73:6f5021cbe752 55
ocomeni 73:6f5021cbe752 56 printf("\n----- HTTP GET response -----\n");
ocomeni 73:6f5021cbe752 57 dump_response(get_res);
ocomeni 73:6f5021cbe752 58
ocomeni 73:6f5021cbe752 59 delete get_req;
ocomeni 73:6f5021cbe752 60 }
ocomeni 73:6f5021cbe752 61
ocomeni 73:6f5021cbe752 62 // POST request to httpbin.org
ocomeni 73:6f5021cbe752 63 {
ocomeni 73:6f5021cbe752 64 HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://httpbin.org/post");
ocomeni 73:6f5021cbe752 65 post_req->set_header("Content-Type", "application/json");
ocomeni 73:6f5021cbe752 66
ocomeni 73:6f5021cbe752 67 const char body[] = "{\"hello\":\"world\"}";
ocomeni 73:6f5021cbe752 68
ocomeni 73:6f5021cbe752 69 HttpResponse* post_res = post_req->send(body, strlen(body));
ocomeni 73:6f5021cbe752 70 if (!post_res) {
ocomeni 73:6f5021cbe752 71 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
ocomeni 73:6f5021cbe752 72 return 1;
ocomeni 73:6f5021cbe752 73 }
ocomeni 73:6f5021cbe752 74
ocomeni 73:6f5021cbe752 75 printf("\n----- HTTP POST response -----\n");
ocomeni 73:6f5021cbe752 76 dump_response(post_res);
ocomeni 73:6f5021cbe752 77
ocomeni 73:6f5021cbe752 78 delete post_req;
ocomeni 73:6f5021cbe752 79 }
ocomeni 73:6f5021cbe752 80
ocomeni 73:6f5021cbe752 81 socket->close();
ocomeni 73:6f5021cbe752 82 delete socket;
ocomeni 73:6f5021cbe752 83
ocomeni 73:6f5021cbe752 84 wait(osWaitForever);
ocomeni 73:6f5021cbe752 85 }
ocomeni 73:6f5021cbe752 86
ocomeni 73:6f5021cbe752 87 #endif