HTTPClient test for IoT Workshop
Dependencies: UbloxUSBModem mbed
Fork of UbloxModemHTTPClientTest by
httptest.cpp@6:8cf840145f92, 2014-02-03 (annotated)
- Committer:
- sam_grove
- Date:
- Mon Feb 03 14:35:03 2014 +0000
- Revision:
- 6:8cf840145f92
- Parent:
- 3:03b3ef627772
update to make CDMA default
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 1:0112fc45285a | 1 | #include "mbed.h" |
bogdanm | 1:0112fc45285a | 2 | #include "CellularModem.h" |
bogdanm | 1:0112fc45285a | 3 | #include "HTTPClient.h" |
bogdanm | 1:0112fc45285a | 4 | #include "httptest.h" |
bogdanm | 1:0112fc45285a | 5 | |
bogdanm | 1:0112fc45285a | 6 | int httptest(CellularModem& modem, const char* apn, const char* username, const char* password) |
bogdanm | 1:0112fc45285a | 7 | { |
bogdanm | 1:0112fc45285a | 8 | printf("Connecting...\n"); |
bogdanm | 1:0112fc45285a | 9 | |
bogdanm | 1:0112fc45285a | 10 | HTTPClient http; |
bogdanm | 1:0112fc45285a | 11 | char str[512]; |
bogdanm | 1:0112fc45285a | 12 | |
mbed_official | 3:03b3ef627772 | 13 | modem.power(true); |
mbed_official | 3:03b3ef627772 | 14 | Thread::wait(1000); |
bogdanm | 1:0112fc45285a | 15 | int ret = modem.connect(apn, username, password); |
bogdanm | 1:0112fc45285a | 16 | if(ret) |
bogdanm | 1:0112fc45285a | 17 | { |
bogdanm | 1:0112fc45285a | 18 | printf("Could not connect\n"); |
bogdanm | 1:0112fc45285a | 19 | return false; |
bogdanm | 1:0112fc45285a | 20 | } |
bogdanm | 1:0112fc45285a | 21 | |
bogdanm | 1:0112fc45285a | 22 | //GET data |
bogdanm | 1:0112fc45285a | 23 | printf("Trying to fetch page...\n"); |
bogdanm | 1:0112fc45285a | 24 | ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
bogdanm | 1:0112fc45285a | 25 | if (!ret) |
bogdanm | 1:0112fc45285a | 26 | { |
bogdanm | 1:0112fc45285a | 27 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
bogdanm | 1:0112fc45285a | 28 | printf("Result: %s\n", str); |
bogdanm | 1:0112fc45285a | 29 | } |
bogdanm | 1:0112fc45285a | 30 | else |
bogdanm | 1:0112fc45285a | 31 | { |
bogdanm | 1:0112fc45285a | 32 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
bogdanm | 1:0112fc45285a | 33 | modem.disconnect(); |
bogdanm | 1:0112fc45285a | 34 | return false; |
bogdanm | 1:0112fc45285a | 35 | } |
bogdanm | 1:0112fc45285a | 36 | |
bogdanm | 1:0112fc45285a | 37 | //POST data |
bogdanm | 1:0112fc45285a | 38 | HTTPMap map; |
bogdanm | 1:0112fc45285a | 39 | HTTPText text(str, 512); |
bogdanm | 1:0112fc45285a | 40 | map.put("Hello", "World"); |
bogdanm | 1:0112fc45285a | 41 | map.put("test", "1234"); |
bogdanm | 1:0112fc45285a | 42 | printf("Trying to post data...\n"); |
bogdanm | 1:0112fc45285a | 43 | ret = http.post("http://httpbin.org/post", map, &text); |
bogdanm | 1:0112fc45285a | 44 | if (!ret) |
bogdanm | 1:0112fc45285a | 45 | { |
bogdanm | 1:0112fc45285a | 46 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
bogdanm | 1:0112fc45285a | 47 | printf("Result: %s\n", str); |
bogdanm | 1:0112fc45285a | 48 | } |
bogdanm | 1:0112fc45285a | 49 | else |
bogdanm | 1:0112fc45285a | 50 | { |
bogdanm | 1:0112fc45285a | 51 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
bogdanm | 1:0112fc45285a | 52 | modem.disconnect(); |
bogdanm | 1:0112fc45285a | 53 | return false; |
bogdanm | 1:0112fc45285a | 54 | } |
bogdanm | 1:0112fc45285a | 55 | |
bogdanm | 1:0112fc45285a | 56 | modem.disconnect(); |
bogdanm | 1:0112fc45285a | 57 | return true; |
bogdanm | 1:0112fc45285a | 58 | } |