http client example

Dependencies:   C027_Support HTTPClient mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

main.cpp

Committer:
mazgch
Date:
2014-05-13
Revision:
5:a18ddbfd70c9
Parent:
4:7fd97087e573
Child:
6:6ff6061a0f76

File content as of revision 5:a18ddbfd70c9:

#include "mbed.h"
#include "HTTPClient.h"
#include "C027.h"
#include "MDM.h"

//----------------------------------------------------------------------
// You may need to configure these parameters

/** Set your secret SIM pin here "1234"
*/
#define SIMPIN      NULL

/** The APN of your network operator, sometimes it is "internet" 
    check your contract with the network operator
*/
#define APN         "gprs.swisscom.ch"

/** Set the user name for your APN, or NULL if not needed
*/
#define USERNAME    NULL

/** Set the password for your APN, or NULL if not needed
*/
#define PASSWORD    NULL 

C027 c027;

char str[512];

int main() 
{
    // turn on the supplies of the Modem
    c027.mdmPower(true);
    printf("Modem Initialize\r\n");
    MDMSerial mdm;
    if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true))
        return -1;
    
    HTTPClient http;
    
    //GET data
    printf("\nTrying to fetch page...\n");
    int 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());
    }
    
    //POST data
    HTTPMap map;
    HTTPText inText(str, 512);
    map.put("Hello", "World");
    map.put("test", "1234");
    printf("\nTrying to post data...\n");
    ret = http.post("http://httpbin.org/post", map, &inText);
    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());
    }
    
    //PUT data
    strcpy(str, "This is a PUT test!");
    HTTPText outText(str);
    //HTTPText inText(str, 512);
    printf("\nTrying to put resource...\n");
    ret = http.put("http://httpbin.org/put", outText, &inText);
    if (!ret)
    {
      printf("Executed PUT 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());
    }
    
    //DELETE data
    //HTTPText inText(str, 512);
    printf("\nTrying to delete resource...\n");
    ret = http.del("http://httpbin.org/delete", &inText);
    if (!ret)
    {
      printf("Executed DELETE 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());
    }

    mdm.disconnect();
    mdm.powerOff();
    c027.mdmPower(false);
    printf("Done\n");
    
    while(true);
}