]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/net/cellular/http/common/httptest.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / tests / net / cellular / http / common / httptest.cpp
1 #include "mbed.h"
2 #include "CellularModem.h"
3 #include "HTTPClient.h"
4 #include "httptest.h"
5
6 int httptest(CellularModem& modem, const char* apn, const char* username, const char* password)
7 {
8 printf("Connecting...\n");
9
10 HTTPClient http;
11 char str[512];
12
13 modem.power(true);
14 Thread::wait(1000);
15 int ret = modem.connect(apn, username, password);
16 if(ret)
17 {
18 printf("Could not connect\n");
19 return false;
20 }
21
22 //GET data
23 printf("Trying to fetch page...\n");
24 ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
25 if (!ret)
26 {
27 printf("Page fetched successfully - read %d characters\n", strlen(str));
28 printf("Result: %s\n", str);
29 }
30 else
31 {
32 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
33 modem.disconnect();
34 return false;
35 }
36
37 //POST data
38 HTTPMap map;
39 HTTPText text(str, 512);
40 map.put("Hello", "World");
41 map.put("test", "1234");
42 printf("Trying to post data...\n");
43 ret = http.post("http://httpbin.org/post", map, &text);
44 if (!ret)
45 {
46 printf("Executed POST successfully - read %d characters\n", strlen(str));
47 printf("Result: %s\n", str);
48 }
49 else
50 {
51 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
52 modem.disconnect();
53 return false;
54 }
55
56 modem.disconnect();
57 return true;
58 }
Imprint / Impressum