Update library (02 Feb 2015)

Dependencies:   EthernetInterface HTTPClient mbed-rtos mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "HTTPClient.h"
00004 
00005 EthernetInterface eth;
00006 HTTPClient http;
00007 char str[512];
00008 
00009 int main()
00010 {
00011     int ret = eth.init(); //Use DHCP
00012     if (!ret) {
00013         printf("Initialized, MAC: %s\n", eth.getMACAddress());
00014     } else {
00015         printf("Error eth.init() - ret = %d\n", ret);
00016         return -1;
00017     }
00018 
00019     ret = eth.connect();
00020     if (!ret) {
00021         printf("Connected, IP: %s, MASK: %s, GW: %s\n",
00022                eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00023     } else {
00024         printf("Error eth.connect() - ret = %d\n", ret);
00025         return -1;
00026     }
00027 
00028 
00029     // GET data
00030     {
00031         printf("\nTrying to GET request...\n");
00032         ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", str, sizeof(str));
00033         if (!ret) {
00034             printf("Page fetched successfully - read %d characters\n", strlen(str));
00035             printf("Result: %s\n", str);
00036         } else {
00037             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00038         }
00039     }
00040 
00041     // POST data
00042     {
00043         HTTPMap map;
00044         HTTPText inText(str, sizeof(str));
00045         map.put("Hello", "World");
00046         map.put("test", "1234");
00047 
00048         printf("\nTrying to POST request...\n");
00049         ret = http.post("http://httpbin.org/post", map, &inText);
00050         if (!ret) {
00051             printf("Executed POST successfully - read %d characters\n", strlen(str));
00052             printf("Result: %s\n", str);
00053         } else {
00054             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00055         }
00056     }
00057 
00058     // PUT data
00059     {
00060         strcpy(str, "This is a PUT test!");
00061         HTTPText outText(str);
00062         HTTPText inText(str, sizeof(str));
00063 
00064         printf("\nTrying to PUT request...\n");
00065         ret = http.put("http://httpbin.org/put", outText, &inText);
00066         if (!ret) {
00067             printf("Executed PUT successfully - read %d characters\n", strlen(str));
00068             printf("Result: %s\n", str);
00069         } else {
00070             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00071         }
00072     }
00073 
00074     // DELETE data
00075     {
00076         HTTPText inText(str, sizeof(str));
00077 
00078         printf("\nTrying to DELETE request...\n");
00079         ret = http.del("http://httpbin.org/delete", &inText);
00080         if (!ret) {
00081             printf("Executed DELETE successfully - read %d characters\n", strlen(str));
00082             printf("Result: %s\n", str);
00083         } else {
00084             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00085         }
00086     }
00087 
00088 
00089     printf("\n");
00090 
00091     ret = eth.disconnect();
00092     if (!ret) {
00093         printf("Disconnected\n");
00094     } else {
00095         printf("Error eth.disconnect() - ret = %d\n", ret);
00096     }
00097 
00098 
00099     while(1) {
00100     }
00101 }