this is a demo code for HTTPClient_GPRS library

Dependencies:   GPRSInterface HTTPClient_GPRS mbed USBDevice

Fork of Seeed_HTTPClient_GPRSInterface_HelloWorld by wei zou

main.cpp

Committer:
lawliet
Date:
2014-02-27
Revision:
0:cc0bec77f305
Child:
1:16498811e319

File content as of revision 0:cc0bec77f305:

#include "mbed.h"
#include "GPRSInterface.h"
#include "HTTPClient.h"

#define TEST_HTTP_GET       1
#define TEST_HTTP_POST      1
#define TEST_HTTP_PUT       1
#define TEST_HTTP_DELETE    1

#define PIN_TX              P0_0
#define PIN_RX              P0_1

GPRSInterface gprs(PIN_TX,PIN_RX,19200,"cmnet",NULL,NULL);
HTTPClient http;
char str[512];

int main()
{
    gprs.init();

    while(false == gprs.connect()) {
        printf("gprs connect error\n");
        wait(2);
    }

    // successful DHCP
    printf("IP Address is %s\n", gprs.getIPAddress());

    int ret;
    HTTPMap map;
    HTTPText inText(str, 512);
    HTTPText outText(str);

#if TEST_HTTP_GET
    //GET data
    printf("\nTrying to fetch page...\n");
    ret = http.get("http://mbed.org/media/uploads/mbed_official/hello.txt", str, 512);
    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());
    }
#endif

#if TEST_HTTP_POST
    //POST data
    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\n");
    } else {
        printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
    }
#endif

#if TEST_HTTP_PUT
    //PUT data
    strcpy(str, "This is a PUT test!");
    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());
    }
#endif

#if TEST_HTTP_DELETE
    //DELETE data
    printf("\nTrying to delete resource...\n");
    ret = http.del("http://httpbin.org/delete", &inText);
    if (!ret) {
        printf("Executed DELETE successfully\n");
    } else {
        printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
    }
#endif

    gprs.disconnect();

    return 0;
}