Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of http-example by
Diff: source/main-http.cpp
- Revision:
- 5:42f713b19337
- Parent:
- 3:0f5859a9e3de
- Child:
- 7:5d32909f77de
--- a/source/main-http.cpp Thu Feb 23 12:36:59 2017 +0100
+++ b/source/main-http.cpp Thu Feb 23 13:58:24 2017 +0100
@@ -15,7 +15,7 @@
for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
printf("\t%s: %s\n", res->get_headers_fields()[ix].c_str(), res->get_headers_values()[ix].c_str());
}
- printf("\nBody:\n\n%s\n", res->get_body().c_str());
+ printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
int main() {
@@ -28,38 +28,42 @@
}
// Do a GET request to httpbin.org
- HttpRequest get_req(network, HTTP_GET, "http://httpbin.org/status/418");
+ {
+ HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418");
- // By default the body is automatically parsed and stored in a string, this is memory heavy.
- // To receive chunked response, pass in a callback as third parameter to 'send'.
- HttpResponse* get_res = get_req.send();
- if (!get_res) {
- printf("HttpRequest failed (error code %d)\n", get_req.get_error());
- return 1;
+ // By default the body is automatically parsed and stored in a string, this is memory heavy.
+ // To receive chunked response, pass in a callback as third parameter to 'send'.
+ HttpResponse* get_res = get_req->send();
+ if (!get_res) {
+ printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+ return 1;
+ }
+
+ printf("\n----- HTTP GET response -----\n");
+ dump_response(get_res);
+
+ delete get_req;
}
- printf("\n----- HTTP GET response -----\n");
- dump_response(get_res);
-
- delete get_res;
-
// POST request to httpbin.org
- HttpRequest post_req(network, HTTP_POST, "http://httpbin.org/post");
- post_req.set_header("Content-Type", "application/json");
+ {
+ HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
+ post_req->set_header("Content-Type", "application/json");
- const char body[] = "{\"hello\":\"world\"}";
+ const char body[] = "{\"hello\":\"world\"}";
- HttpResponse* post_res = post_req.send(body, strlen(body));
- if (!post_res) {
- printf("HttpRequest failed (error code %d)\n", post_req.get_error());
- return 1;
+ HttpResponse* post_res = post_req->send(body, strlen(body));
+ if (!post_res) {
+ printf("HttpRequest failed (error code %d)\n", post_req->get_error());
+ return 1;
+ }
+
+ printf("\n----- HTTP POST response -----\n");
+ dump_response(post_res);
+
+ delete post_req;
}
- printf("\n----- HTTP POST response -----\n");
- dump_response(post_res);
-
- delete post_res;
-
Thread::wait(osWaitForever);
}
