Fork of SandBox's original mbed-http (https://os.mbed.com/teams/sandbox/code/mbed-http/) and update for MbedOS6+ Content of TESTS folder was replaced with basic examples form original SandBox's HelloWorld

Committer:
JohnnyK
Date:
Sun May 30 19:45:32 2021 +0000
Revision:
41:cc1a8c63f159
change test program to two basic example from SandBox's helloworld program ofc reworked.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 41:cc1a8c63f159 1 #if 0 /*Set it to "1" to enable content*/
JohnnyK 41:cc1a8c63f159 2
JohnnyK 41:cc1a8c63f159 3 #include "mbed.h"
JohnnyK 41:cc1a8c63f159 4 #include "http_request.h"
JohnnyK 41:cc1a8c63f159 5 #include "mbed_mem_trace.h"
JohnnyK 41:cc1a8c63f159 6
JohnnyK 41:cc1a8c63f159 7 void dump_response(HttpResponse* res) {
JohnnyK 41:cc1a8c63f159 8 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
JohnnyK 41:cc1a8c63f159 9
JohnnyK 41:cc1a8c63f159 10 printf("Headers:\n");
JohnnyK 41:cc1a8c63f159 11 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
JohnnyK 41:cc1a8c63f159 12 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
JohnnyK 41:cc1a8c63f159 13 }
JohnnyK 41:cc1a8c63f159 14 printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
JohnnyK 41:cc1a8c63f159 15 }
JohnnyK 41:cc1a8c63f159 16
JohnnyK 41:cc1a8c63f159 17 int main() {
JohnnyK 41:cc1a8c63f159 18 printf("Example of a HTTP client\n");
JohnnyK 41:cc1a8c63f159 19 // Connect to the network with the default networking interface
JohnnyK 41:cc1a8c63f159 20 // if you use WiFi: see mbed_app.json for the credentials
JohnnyK 41:cc1a8c63f159 21 printf("[NWK] Connecting to network...\n");
JohnnyK 41:cc1a8c63f159 22 NetworkInterface* network = NetworkInterface::get_default_instance();
JohnnyK 41:cc1a8c63f159 23 if (!network) {
JohnnyK 41:cc1a8c63f159 24 printf("[NWK] No network interface found, select an interface in 'network-helper.h'\n");
JohnnyK 41:cc1a8c63f159 25 return 1;
JohnnyK 41:cc1a8c63f159 26 }
JohnnyK 41:cc1a8c63f159 27 nsapi_error_t err;
JohnnyK 41:cc1a8c63f159 28 if ((err = network->connect())!= NSAPI_ERROR_OK) {
JohnnyK 41:cc1a8c63f159 29 printf("[NWK] Failed to connect to network (%d)\n", err);
JohnnyK 41:cc1a8c63f159 30 return 1;
JohnnyK 41:cc1a8c63f159 31 }
JohnnyK 41:cc1a8c63f159 32 printf("[NWK] Connected to the network\n");
JohnnyK 41:cc1a8c63f159 33 SocketAddress ip;
JohnnyK 41:cc1a8c63f159 34 network->get_ip_address(&ip);
JohnnyK 41:cc1a8c63f159 35 printf("[NWK] IP address: %s\n", ip.get_ip_address());
JohnnyK 41:cc1a8c63f159 36
JohnnyK 41:cc1a8c63f159 37
JohnnyK 41:cc1a8c63f159 38 // Do a GET request to httpbin.org
JohnnyK 41:cc1a8c63f159 39 {
JohnnyK 41:cc1a8c63f159 40 // By default the body is automatically parsed and stored in a buffer, this is memory heavy.
JohnnyK 41:cc1a8c63f159 41 // To receive chunked response, pass in a callback as last parameter to the constructor.
JohnnyK 41:cc1a8c63f159 42 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418");
JohnnyK 41:cc1a8c63f159 43
JohnnyK 41:cc1a8c63f159 44 HttpResponse* get_res = get_req->send();
JohnnyK 41:cc1a8c63f159 45 if (!get_res) {
JohnnyK 41:cc1a8c63f159 46 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
JohnnyK 41:cc1a8c63f159 47 return 1;
JohnnyK 41:cc1a8c63f159 48 }
JohnnyK 41:cc1a8c63f159 49
JohnnyK 41:cc1a8c63f159 50 printf("\n----- HTTP GET response -----\n");
JohnnyK 41:cc1a8c63f159 51 dump_response(get_res);
JohnnyK 41:cc1a8c63f159 52
JohnnyK 41:cc1a8c63f159 53 delete get_req;
JohnnyK 41:cc1a8c63f159 54 }
JohnnyK 41:cc1a8c63f159 55
JohnnyK 41:cc1a8c63f159 56 // POST request to httpbin.org
JohnnyK 41:cc1a8c63f159 57 {
JohnnyK 41:cc1a8c63f159 58 HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
JohnnyK 41:cc1a8c63f159 59 post_req->set_header("Content-Type", "application/json");
JohnnyK 41:cc1a8c63f159 60
JohnnyK 41:cc1a8c63f159 61 const char body[] = "{\"hello\":\"world\"}";
JohnnyK 41:cc1a8c63f159 62
JohnnyK 41:cc1a8c63f159 63 HttpResponse* post_res = post_req->send(body, strlen(body));
JohnnyK 41:cc1a8c63f159 64 if (!post_res) {
JohnnyK 41:cc1a8c63f159 65 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
JohnnyK 41:cc1a8c63f159 66 return 1;
JohnnyK 41:cc1a8c63f159 67 }
JohnnyK 41:cc1a8c63f159 68
JohnnyK 41:cc1a8c63f159 69 printf("\n----- HTTP POST response -----\n");
JohnnyK 41:cc1a8c63f159 70 dump_response(post_res);
JohnnyK 41:cc1a8c63f159 71
JohnnyK 41:cc1a8c63f159 72 delete post_req;
JohnnyK 41:cc1a8c63f159 73 }
JohnnyK 41:cc1a8c63f159 74
JohnnyK 41:cc1a8c63f159 75 while(osWaitForever);
JohnnyK 41:cc1a8c63f159 76 }
JohnnyK 41:cc1a8c63f159 77 #endif