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