Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of Sample_HTTPClient by
main.cpp
00001 /** Beispiel fuer Verwendung der HTTP Methoden (GET, POST, PUT, DELETE) 00002 * 00003 * Braucht ein Board mit Ethernet 00004 */ 00005 #include "mbed.h" 00006 #include "HTTPClient.h" 00007 #include "HTTPText.h" 00008 #include "EthernetInterface.h" 00009 00010 Serial pc(USBTX, USBRX); 00011 00012 EthernetInterface eth; 00013 HTTPClient http; 00014 char message[6000]; 00015 char str[512]; 00016 00017 DigitalOut myled(LED1); 00018 00019 int main() 00020 { 00021 pc.baud(9600); 00022 printf("HTTP Client - Build " __DATE__ " - " __TIME__ "\r\n"); 00023 eth.init(); 00024 eth.connect(); 00025 00026 while(1) 00027 { 00028 myled = 1; 00029 printf( "GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt\n" ); 00030 //int ret = http.get("https://raw.githubusercontent.com/mc-b/microHOME/master/README.md", message, sizeof(message)); 00031 int ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", message, sizeof(message)); 00032 if ( !ret ) 00033 { 00034 printf("Success - read %d characters.\r\n", strlen(message)); 00035 printf("%s\r\n", message); 00036 } 00037 else 00038 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00039 00040 myled = 0; 00041 00042 //POST data 00043 HTTPMap map; 00044 HTTPText inText(str, 512); 00045 map.put("Hello", "World"); 00046 map.put("test", "1234"); 00047 printf("\nTrying to post data...\n"); 00048 ret = http.post("http://httpbin.org/post", map, &inText); 00049 if ( ! ret ) 00050 { 00051 printf("Executed POST successfully - read %d characters\n", strlen(str)); 00052 printf("Result: %s\n", str); 00053 } 00054 else 00055 { 00056 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00057 } 00058 00059 //PUT data 00060 strcpy(str, "This is a PUT test!"); 00061 HTTPText outText(str); 00062 //HTTPText inText(str, 512); 00063 printf("\nTrying to put resource...\n"); 00064 ret = http.put("http://httpbin.org/put", outText, &inText); 00065 if (!ret) { 00066 printf("Executed PUT successfully - read %d characters\n", strlen(str)); 00067 printf("Result: %s\n", str); 00068 } else { 00069 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00070 } 00071 00072 //DELETE data 00073 //HTTPText inText(str, 512); 00074 printf("\nTrying to delete resource...\n"); 00075 ret = http.del("http://httpbin.org/delete", &inText); 00076 if (!ret) { 00077 printf("Executed DELETE successfully - read %d characters\n", strlen(str)); 00078 printf("Result: %s\n", str); 00079 } else { 00080 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00081 } 00082 00083 wait(10); 00084 } 00085 // eth.disconnect(); 00086 }
Generated on Wed Jul 13 2022 16:23:30 by
1.7.2
