this is using the mbed os version 5-13-1
source/main-http.cpp@73:6f5021cbe752, 2019-02-28 (annotated)
- Committer:
- ocomeni
- Date:
- Thu Feb 28 18:13:48 2019 +0000
- Revision:
- 73:6f5021cbe752
commit current application code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ocomeni | 73:6f5021cbe752 | 1 | #include "select-demo.h" |
| ocomeni | 73:6f5021cbe752 | 2 | |
| ocomeni | 73:6f5021cbe752 | 3 | #if DEMO == DEMO_HTTP |
| ocomeni | 73:6f5021cbe752 | 4 | |
| ocomeni | 73:6f5021cbe752 | 5 | #include "mbed.h" |
| ocomeni | 73:6f5021cbe752 | 6 | #include "http_request.h" |
| ocomeni | 73:6f5021cbe752 | 7 | #include "network-helper.h" |
| ocomeni | 73:6f5021cbe752 | 8 | #include "mbed_mem_trace.h" |
| ocomeni | 73:6f5021cbe752 | 9 | |
| ocomeni | 73:6f5021cbe752 | 10 | void dump_response(HttpResponse* res) { |
| ocomeni | 73:6f5021cbe752 | 11 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
| ocomeni | 73:6f5021cbe752 | 12 | |
| ocomeni | 73:6f5021cbe752 | 13 | printf("Headers:\n"); |
| ocomeni | 73:6f5021cbe752 | 14 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
| ocomeni | 73:6f5021cbe752 | 15 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
| ocomeni | 73:6f5021cbe752 | 16 | } |
| ocomeni | 73:6f5021cbe752 | 17 | printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
| ocomeni | 73:6f5021cbe752 | 18 | } |
| ocomeni | 73:6f5021cbe752 | 19 | |
| ocomeni | 73:6f5021cbe752 | 20 | int main() { |
| ocomeni | 73:6f5021cbe752 | 21 | // Connect to the network with the default networking interface |
| ocomeni | 73:6f5021cbe752 | 22 | // if you use WiFi: see mbed_app.json for the credentials |
| ocomeni | 73:6f5021cbe752 | 23 | NetworkInterface* network = connect_to_default_network_interface(); |
| ocomeni | 73:6f5021cbe752 | 24 | if (!network) { |
| ocomeni | 73:6f5021cbe752 | 25 | printf("Cannot connect to the network, see serial output\n"); |
| ocomeni | 73:6f5021cbe752 | 26 | return 1; |
| ocomeni | 73:6f5021cbe752 | 27 | } |
| ocomeni | 73:6f5021cbe752 | 28 | |
| ocomeni | 73:6f5021cbe752 | 29 | // Do a GET request to httpbin.org |
| ocomeni | 73:6f5021cbe752 | 30 | { |
| ocomeni | 73:6f5021cbe752 | 31 | // By default the body is automatically parsed and stored in a buffer, this is memory heavy. |
| ocomeni | 73:6f5021cbe752 | 32 | // To receive chunked response, pass in a callback as last parameter to the constructor. |
| ocomeni | 73:6f5021cbe752 | 33 | HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); |
| ocomeni | 73:6f5021cbe752 | 34 | |
| ocomeni | 73:6f5021cbe752 | 35 | HttpResponse* get_res = get_req->send(); |
| ocomeni | 73:6f5021cbe752 | 36 | if (!get_res) { |
| ocomeni | 73:6f5021cbe752 | 37 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
| ocomeni | 73:6f5021cbe752 | 38 | return 1; |
| ocomeni | 73:6f5021cbe752 | 39 | } |
| ocomeni | 73:6f5021cbe752 | 40 | |
| ocomeni | 73:6f5021cbe752 | 41 | printf("\n----- HTTP GET response -----\n"); |
| ocomeni | 73:6f5021cbe752 | 42 | dump_response(get_res); |
| ocomeni | 73:6f5021cbe752 | 43 | |
| ocomeni | 73:6f5021cbe752 | 44 | delete get_req; |
| ocomeni | 73:6f5021cbe752 | 45 | } |
| ocomeni | 73:6f5021cbe752 | 46 | |
| ocomeni | 73:6f5021cbe752 | 47 | // POST request to httpbin.org |
| ocomeni | 73:6f5021cbe752 | 48 | { |
| ocomeni | 73:6f5021cbe752 | 49 | HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); |
| ocomeni | 73:6f5021cbe752 | 50 | post_req->set_header("Content-Type", "application/json"); |
| ocomeni | 73:6f5021cbe752 | 51 | |
| ocomeni | 73:6f5021cbe752 | 52 | const char body[] = "{\"hello\":\"world\"}"; |
| ocomeni | 73:6f5021cbe752 | 53 | |
| ocomeni | 73:6f5021cbe752 | 54 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
| ocomeni | 73:6f5021cbe752 | 55 | if (!post_res) { |
| ocomeni | 73:6f5021cbe752 | 56 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
| ocomeni | 73:6f5021cbe752 | 57 | return 1; |
| ocomeni | 73:6f5021cbe752 | 58 | } |
| ocomeni | 73:6f5021cbe752 | 59 | |
| ocomeni | 73:6f5021cbe752 | 60 | printf("\n----- HTTP POST response -----\n"); |
| ocomeni | 73:6f5021cbe752 | 61 | dump_response(post_res); |
| ocomeni | 73:6f5021cbe752 | 62 | |
| ocomeni | 73:6f5021cbe752 | 63 | delete post_req; |
| ocomeni | 73:6f5021cbe752 | 64 | } |
| ocomeni | 73:6f5021cbe752 | 65 | |
| ocomeni | 73:6f5021cbe752 | 66 | wait(osWaitForever); |
| ocomeni | 73:6f5021cbe752 | 67 | } |
| ocomeni | 73:6f5021cbe752 | 68 | |
| ocomeni | 73:6f5021cbe752 | 69 | #endif |