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
Diff: README.md
- Revision:
- 5:2456c90f02e9
- Parent:
- 0:910f5949759f
- Child:
- 12:530c2ebee349
--- a/README.md Thu Feb 23 13:50:43 2017 +0100 +++ b/README.md Thu Feb 23 13:56:17 2017 +0100 @@ -9,13 +9,15 @@ const char body[] = "{\"hello\":\"world\"}"; -HttpRequest request(network, HTTP_POST, "http://httpbin.org/post"); -request.set_header("Content-Type", "application/json); -HttpResponse* response = request.send(body, strlen(body)); +HttpRequest* request = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); +request->set_header("Content-Type", "application/json"); +HttpResponse* response = request->send(body, strlen(body)); // if response is NULL, check response->get_error() printf("status is %d - %s\n", response->get_status_code(), response->get_status_message()); -printf("body is:\n%s\n", response->get_body().c_str()); +printf("body is:\n%s\n", response->get_body_as_string().c_str()); + +delete request; // also clears out the response ``` ## HTTPS Request API @@ -29,24 +31,27 @@ const char body[] = "{\"hello\":\"world\"}"; -HttpsRequest request(network, SSL_CA_PEM, HTTP_GET "https://httpbin.org/status/418"); -HttpResponse* response = request.send(); +HttpsRequest* request = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET "https://httpbin.org/status/418"); +HttpResponse* response = request->send(); // if response is NULL, check response->get_error() printf("status is %d - %s\n", response->get_status_code(), response->get_status_message()); printf("body is:\n%s\n", response->get_body().c_str()); + +delete request; ``` ## Dealing with large body -By default the library will store the full request body on the stack. This works well for small responses, but you'll run out of memory when receiving a large response body. To mitigate this you can pass in a callback as the last argument to the request constructor. This callback will be called whenever a chunk of the body is received. You can set the request chunk size in the `HTTP_RECEIVE_BUFFER_SIZE` macro (see `mbed_lib.json` for the definition). +By default the library will store the full request body on the heap. This works well for small responses, but you'll run out of memory when receiving a large response body. To mitigate this you can pass in a callback as the last argument to the request constructor. This callback will be called whenever a chunk of the body is received. You can set the request chunk size in the `HTTP_RECEIVE_BUFFER_SIZE` macro (see `mbed_lib.json` for the definition) although it also depends on the buffer size of the underlying network connection. ```cpp void body_callback(const char* data, size_t data_len) { // do something with the data } -HttpRequest req(network, HTTP_GET, "http://pathtolargefile.com"); +HttpRequest* req = new HttpRequest(network, HTTP_GET, "http://pathtolargefile.com"); +req->send(NULL, 0, body_callback); ``` ## Tested on