]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/net/protocols/HTTPClient_HelloWorld/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / net / protocols / HTTPClient_HelloWorld / main.cpp
1 #include "mbed.h"
2 #include "test_env.h"
3 #include "EthernetInterface.h"
4 #include "HTTPClient.h"
5
6
7 namespace {
8     const int BUFFER_SIZE = 512;
9 }
10
11 int main() {
12     MBED_HOSTTEST_TIMEOUT(15);
13     MBED_HOSTTEST_SELECT(default_auto);
14     MBED_HOSTTEST_DESCRIPTION(HTTP client hello world);
15     MBED_HOSTTEST_START("NET_7");
16
17     char http_request_buffer[BUFFER_SIZE + 1] = {0};
18     HTTPClient http;
19     EthernetInterface eth;
20     eth.init(); //Use DHCP
21     eth.connect();
22
23     //GET data
24     {
25         bool result = true;
26         const char *url_hello_txt = "http://developer.mbed.org/media/uploads/donatien/hello.txt";
27         printf("HTTP_GET: Trying to fetch page '%s'...\r\n", url_hello_txt);
28         HTTPResult ret = http.get(url_hello_txt, http_request_buffer, BUFFER_SIZE);
29         if (ret == HTTP_OK) {
30             printf("HTTP_GET: Read %d chars: '%s' ... [OK]\r\n", strlen(http_request_buffer), http_request_buffer);
31         } else {
32             printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
33             result = false;
34         }
35
36         if (result == false) {
37             eth.disconnect();
38             MBED_HOSTTEST_RESULT(false);
39         }
40     }
41
42     //POST data
43     {
44         bool result = true;
45         const char *url_httpbin_post = "http://httpbin.org/post";
46         HTTPText text(http_request_buffer, BUFFER_SIZE);
47         HTTPMap map;
48         map.put("Hello", "World");
49         map.put("test", "1234");
50         printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post);
51         HTTPResult ret = http.post(url_httpbin_post, map, &text);
52         if (ret == HTTP_OK) {
53             printf("HTTP_POST: Read %d chars ... [OK]\r\n", strlen(http_request_buffer));
54             printf("HTTP_POST: %s\r\n", http_request_buffer);
55         } else {
56             printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
57             result = false;
58         }
59
60         if (result == false) {
61             eth.disconnect();
62             MBED_HOSTTEST_RESULT(false);
63         }
64     }
65     eth.disconnect();
66     MBED_HOSTTEST_RESULT(true);
67 }