Simple example demonstrating how to use GET & POST requests with the HTTP Client

Dependencies:   EthernetInterface HTTPClient mbed-rtos mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

Committer:
donatien
Date:
Thu Aug 30 15:42:06 2012 +0000
Revision:
2:270e2d0bb85a
Parent:
1:d263603373ac
Child:
3:a8803b172f6d
Updated library with fix, added example for PUT & DELETE requests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:0e0debc29569 1 #include "mbed.h"
donatien 0:0e0debc29569 2 #include "EthernetInterface.h"
donatien 0:0e0debc29569 3 #include "HTTPClient.h"
donatien 0:0e0debc29569 4
donatien 1:d263603373ac 5 EthernetInterface eth;
donatien 1:d263603373ac 6 HTTPClient http;
donatien 1:d263603373ac 7 char str[512];
donatien 1:d263603373ac 8
donatien 0:0e0debc29569 9 int main()
donatien 0:0e0debc29569 10 {
donatien 0:0e0debc29569 11 eth.init(); //Use DHCP
donatien 0:0e0debc29569 12
donatien 0:0e0debc29569 13 eth.connect();
donatien 0:0e0debc29569 14
donatien 0:0e0debc29569 15 //GET data
donatien 2:270e2d0bb85a 16 printf("\nTrying to fetch page...\n");
donatien 0:0e0debc29569 17 int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
donatien 0:0e0debc29569 18 if (!ret)
donatien 0:0e0debc29569 19 {
donatien 0:0e0debc29569 20 printf("Page fetched successfully - read %d characters\n", strlen(str));
donatien 0:0e0debc29569 21 printf("Result: %s\n", str);
donatien 0:0e0debc29569 22 }
donatien 0:0e0debc29569 23 else
donatien 0:0e0debc29569 24 {
donatien 0:0e0debc29569 25 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
donatien 0:0e0debc29569 26 }
donatien 1:d263603373ac 27
donatien 0:0e0debc29569 28 //POST data
donatien 0:0e0debc29569 29 HTTPMap map;
donatien 2:270e2d0bb85a 30 HTTPText inText(str, 512);
donatien 0:0e0debc29569 31 map.put("Hello", "World");
donatien 0:0e0debc29569 32 map.put("test", "1234");
donatien 2:270e2d0bb85a 33 printf("\nTrying to post data...\n");
donatien 2:270e2d0bb85a 34 ret = http.post("http://httpbin.org/post", map, &inText);
donatien 0:0e0debc29569 35 if (!ret)
donatien 0:0e0debc29569 36 {
donatien 0:0e0debc29569 37 printf("Executed POST successfully - read %d characters\n", strlen(str));
donatien 0:0e0debc29569 38 printf("Result: %s\n", str);
donatien 0:0e0debc29569 39 }
donatien 0:0e0debc29569 40 else
donatien 0:0e0debc29569 41 {
donatien 0:0e0debc29569 42 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
donatien 0:0e0debc29569 43 }
donatien 0:0e0debc29569 44
donatien 2:270e2d0bb85a 45 //PUT data
donatien 2:270e2d0bb85a 46 strcpy(str, "This is a PUT test!");
donatien 2:270e2d0bb85a 47 HTTPText outText(str);
donatien 2:270e2d0bb85a 48 //HTTPText inText(str, 512);
donatien 2:270e2d0bb85a 49 printf("\nTrying to put resource...\n");
donatien 2:270e2d0bb85a 50 ret = http.put("http://httpbin.org/put", outText, &inText);
donatien 2:270e2d0bb85a 51 if (!ret)
donatien 2:270e2d0bb85a 52 {
donatien 2:270e2d0bb85a 53 printf("Executed PUT successfully - read %d characters\n", strlen(str));
donatien 2:270e2d0bb85a 54 printf("Result: %s\n", str);
donatien 2:270e2d0bb85a 55 }
donatien 2:270e2d0bb85a 56 else
donatien 2:270e2d0bb85a 57 {
donatien 2:270e2d0bb85a 58 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
donatien 2:270e2d0bb85a 59 }
donatien 2:270e2d0bb85a 60
donatien 2:270e2d0bb85a 61 //DELETE data
donatien 2:270e2d0bb85a 62 //HTTPText inText(str, 512);
donatien 2:270e2d0bb85a 63 printf("\nTrying to delete resource...\n");
donatien 2:270e2d0bb85a 64 ret = http.del("http://httpbin.org/delete", &inText);
donatien 2:270e2d0bb85a 65 if (!ret)
donatien 2:270e2d0bb85a 66 {
donatien 2:270e2d0bb85a 67 printf("Executed DELETE successfully - read %d characters\n", strlen(str));
donatien 2:270e2d0bb85a 68 printf("Result: %s\n", str);
donatien 2:270e2d0bb85a 69 }
donatien 2:270e2d0bb85a 70 else
donatien 2:270e2d0bb85a 71 {
donatien 2:270e2d0bb85a 72 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
donatien 2:270e2d0bb85a 73 }
donatien 2:270e2d0bb85a 74
donatien 0:0e0debc29569 75 eth.disconnect();
donatien 0:0e0debc29569 76
donatien 0:0e0debc29569 77 while(1) {
donatien 0:0e0debc29569 78 }
donatien 0:0e0debc29569 79 }