u-blox modem HTTP client test

Dependencies:   UbloxUSBModem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers httptest.cpp Source File

httptest.cpp

00001 #include "mbed.h"
00002 #include "CellularModem.h"
00003 #include "HTTPClient.h"
00004 #include "httptest.h"
00005 
00006 int httptest(CellularModem& modem, const char* apn, const char* username, const char* password)
00007 {
00008     printf("Connecting...\n");
00009 
00010     HTTPClient http;
00011     char str[512];
00012 
00013     modem.power(true);
00014     Thread::wait(1000);
00015     int ret = modem.connect(apn, username, password);
00016     if(ret)
00017     {
00018       printf("Could not connect\n");
00019       return false;
00020     }
00021 
00022     //GET data
00023     printf("Trying to fetch page...\n");
00024     ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
00025     if (!ret)
00026     {
00027       printf("Page fetched successfully - read %d characters\n", strlen(str));
00028       printf("Result: %s\n", str);
00029     }
00030     else
00031     {
00032       printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00033       modem.disconnect();
00034       return false;
00035     }
00036 
00037     //POST data
00038     HTTPMap map;
00039     HTTPText text(str, 512);
00040     map.put("Hello", "World");
00041     map.put("test", "1234");
00042     printf("Trying to post data...\n");
00043     ret = http.post("http://httpbin.org/post", map, &text);
00044     if (!ret)
00045     {
00046       printf("Executed POST successfully - read %d characters\n", strlen(str));
00047       printf("Result: %s\n", str);
00048     }
00049     else
00050     {
00051       printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00052       modem.disconnect();
00053       return false;
00054     }
00055 
00056     modem.disconnect();
00057     return true;
00058 }