Beispiel HTTP GET

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of IoTKit_HTTPClient by mc-b

Committer:
marcel1691
Date:
Mon Jan 19 10:39:52 2015 +0000
Revision:
2:c9e058ee6f87
Parent:
1:2e29a33cd918
Child:
3:aad64a4b6ff6
Korrektur key/value Laengen beim lesen Request

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcel1691 1:2e29a33cd918 1 /** Beispiel fuer Verwendung der HTTP Methoden (GET, POST, PUT, DELETE)
marcel1691 1:2e29a33cd918 2 *
marcel1691 1:2e29a33cd918 3 * Braucht ein Board mit Ethernet
marcel1691 1:2e29a33cd918 4 */
WiredHome 0:fb5060c39dd1 5 #include "mbed.h"
WiredHome 0:fb5060c39dd1 6 #include "HTTPClient.h"
marcel1691 1:2e29a33cd918 7 #include "HTTPText.h"
WiredHome 0:fb5060c39dd1 8 #include "EthernetInterface.h"
WiredHome 0:fb5060c39dd1 9
WiredHome 0:fb5060c39dd1 10 Serial pc(USBTX, USBRX);
WiredHome 0:fb5060c39dd1 11
WiredHome 0:fb5060c39dd1 12 EthernetInterface eth;
WiredHome 0:fb5060c39dd1 13 HTTPClient http;
marcel1691 1:2e29a33cd918 14 char message[6000];
marcel1691 1:2e29a33cd918 15 char str[512];
WiredHome 0:fb5060c39dd1 16
WiredHome 0:fb5060c39dd1 17 DigitalOut myled(LED1);
WiredHome 0:fb5060c39dd1 18
marcel1691 1:2e29a33cd918 19 int main()
marcel1691 1:2e29a33cd918 20 {
marcel1691 1:2e29a33cd918 21 pc.baud(9600);
WiredHome 0:fb5060c39dd1 22 printf("HTTP Client - Build " __DATE__ " - " __TIME__ "\r\n");
WiredHome 0:fb5060c39dd1 23 eth.init();
WiredHome 0:fb5060c39dd1 24 eth.connect();
WiredHome 0:fb5060c39dd1 25
marcel1691 1:2e29a33cd918 26 while(1)
marcel1691 1:2e29a33cd918 27 {
WiredHome 0:fb5060c39dd1 28 myled = 1;
marcel1691 1:2e29a33cd918 29 printf( "GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt\n" );
marcel1691 1:2e29a33cd918 30 //int ret = http.get("https://raw.githubusercontent.com/mc-b/microHOME/master/README.md", message, sizeof(message));
marcel1691 1:2e29a33cd918 31 int ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", message, sizeof(message));
marcel1691 1:2e29a33cd918 32 if ( !ret )
marcel1691 1:2e29a33cd918 33 {
WiredHome 0:fb5060c39dd1 34 printf("Success - read %d characters.\r\n", strlen(message));
WiredHome 0:fb5060c39dd1 35 printf("%s\r\n", message);
WiredHome 0:fb5060c39dd1 36 }
marcel1691 1:2e29a33cd918 37 else
marcel1691 1:2e29a33cd918 38 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
marcel1691 1:2e29a33cd918 39
WiredHome 0:fb5060c39dd1 40 myled = 0;
marcel1691 1:2e29a33cd918 41
marcel1691 1:2e29a33cd918 42 //POST data
marcel1691 1:2e29a33cd918 43 HTTPMap map;
marcel1691 1:2e29a33cd918 44 HTTPText inText(str, 512);
marcel1691 1:2e29a33cd918 45 map.put("Hello", "World");
marcel1691 1:2e29a33cd918 46 map.put("test", "1234");
marcel1691 1:2e29a33cd918 47 printf("\nTrying to post data...\n");
marcel1691 1:2e29a33cd918 48 ret = http.post("http://httpbin.org/post", map, &inText);
marcel1691 1:2e29a33cd918 49 if ( ! ret )
marcel1691 1:2e29a33cd918 50 {
marcel1691 1:2e29a33cd918 51 printf("Executed POST successfully - read %d characters\n", strlen(str));
marcel1691 1:2e29a33cd918 52 printf("Result: %s\n", str);
marcel1691 1:2e29a33cd918 53 }
marcel1691 1:2e29a33cd918 54 else
marcel1691 1:2e29a33cd918 55 {
marcel1691 1:2e29a33cd918 56 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
marcel1691 1:2e29a33cd918 57 }
marcel1691 1:2e29a33cd918 58
marcel1691 1:2e29a33cd918 59 //PUT data
marcel1691 1:2e29a33cd918 60 strcpy(str, "This is a PUT test!");
marcel1691 1:2e29a33cd918 61 HTTPText outText(str);
marcel1691 1:2e29a33cd918 62 //HTTPText inText(str, 512);
marcel1691 1:2e29a33cd918 63 printf("\nTrying to put resource...\n");
marcel1691 1:2e29a33cd918 64 ret = http.put("http://httpbin.org/put", outText, &inText);
marcel1691 1:2e29a33cd918 65 if (!ret) {
marcel1691 1:2e29a33cd918 66 printf("Executed PUT successfully - read %d characters\n", strlen(str));
marcel1691 1:2e29a33cd918 67 printf("Result: %s\n", str);
marcel1691 1:2e29a33cd918 68 } else {
marcel1691 1:2e29a33cd918 69 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
marcel1691 1:2e29a33cd918 70 }
marcel1691 1:2e29a33cd918 71
marcel1691 1:2e29a33cd918 72 //DELETE data
marcel1691 1:2e29a33cd918 73 //HTTPText inText(str, 512);
marcel1691 1:2e29a33cd918 74 printf("\nTrying to delete resource...\n");
marcel1691 1:2e29a33cd918 75 ret = http.del("http://httpbin.org/delete", &inText);
marcel1691 1:2e29a33cd918 76 if (!ret) {
marcel1691 1:2e29a33cd918 77 printf("Executed DELETE successfully - read %d characters\n", strlen(str));
marcel1691 1:2e29a33cd918 78 printf("Result: %s\n", str);
marcel1691 1:2e29a33cd918 79 } else {
marcel1691 1:2e29a33cd918 80 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
marcel1691 1:2e29a33cd918 81 }
marcel1691 1:2e29a33cd918 82
WiredHome 0:fb5060c39dd1 83 wait(10);
WiredHome 0:fb5060c39dd1 84 }
WiredHome 0:fb5060c39dd1 85 // eth.disconnect();
WiredHome 0:fb5060c39dd1 86 }