HTTPClient test for IoT Workshop
Dependencies: UbloxUSBModem mbed
Fork of UbloxModemHTTPClientTest by
httptest.cpp@1:0112fc45285a, 2013-10-18 (annotated)
- Committer:
- bogdanm
- Date:
- Fri Oct 18 21:05:40 2013 +0300
- Revision:
- 1:0112fc45285a
- Child:
- 3:03b3ef627772
Initial release of the test program
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 | |
bogdanm | 1:0112fc45285a | 13 | int ret = modem.connect(apn, username, password); |
bogdanm | 1:0112fc45285a | 14 | if(ret) |
bogdanm | 1:0112fc45285a | 15 | { |
bogdanm | 1:0112fc45285a | 16 | printf("Could not connect\n"); |
bogdanm | 1:0112fc45285a | 17 | return false; |
bogdanm | 1:0112fc45285a | 18 | } |
bogdanm | 1:0112fc45285a | 19 | |
bogdanm | 1:0112fc45285a | 20 | //GET data |
bogdanm | 1:0112fc45285a | 21 | printf("Trying to fetch page...\n"); |
bogdanm | 1:0112fc45285a | 22 | ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
bogdanm | 1:0112fc45285a | 23 | if (!ret) |
bogdanm | 1:0112fc45285a | 24 | { |
bogdanm | 1:0112fc45285a | 25 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
bogdanm | 1:0112fc45285a | 26 | printf("Result: %s\n", str); |
bogdanm | 1:0112fc45285a | 27 | } |
bogdanm | 1:0112fc45285a | 28 | else |
bogdanm | 1:0112fc45285a | 29 | { |
bogdanm | 1:0112fc45285a | 30 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
bogdanm | 1:0112fc45285a | 31 | modem.disconnect(); |
bogdanm | 1:0112fc45285a | 32 | return false; |
bogdanm | 1:0112fc45285a | 33 | } |
bogdanm | 1:0112fc45285a | 34 | |
bogdanm | 1:0112fc45285a | 35 | //POST data |
bogdanm | 1:0112fc45285a | 36 | HTTPMap map; |
bogdanm | 1:0112fc45285a | 37 | HTTPText text(str, 512); |
bogdanm | 1:0112fc45285a | 38 | map.put("Hello", "World"); |
bogdanm | 1:0112fc45285a | 39 | map.put("test", "1234"); |
bogdanm | 1:0112fc45285a | 40 | printf("Trying to post data...\n"); |
bogdanm | 1:0112fc45285a | 41 | ret = http.post("http://httpbin.org/post", map, &text); |
bogdanm | 1:0112fc45285a | 42 | if (!ret) |
bogdanm | 1:0112fc45285a | 43 | { |
bogdanm | 1:0112fc45285a | 44 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
bogdanm | 1:0112fc45285a | 45 | printf("Result: %s\n", str); |
bogdanm | 1:0112fc45285a | 46 | } |
bogdanm | 1:0112fc45285a | 47 | else |
bogdanm | 1:0112fc45285a | 48 | { |
bogdanm | 1:0112fc45285a | 49 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
bogdanm | 1:0112fc45285a | 50 | modem.disconnect(); |
bogdanm | 1:0112fc45285a | 51 | return false; |
bogdanm | 1:0112fc45285a | 52 | } |
bogdanm | 1:0112fc45285a | 53 | |
bogdanm | 1:0112fc45285a | 54 | modem.disconnect(); |
bogdanm | 1:0112fc45285a | 55 | return true; |
bogdanm | 1:0112fc45285a | 56 | } |