HTTP/HTTPS client Hello World application running with X-NUCLEO-IDW01M1v2 wifi board.

Dependencies:   HTTPClient NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Fork of HTTPClient_HelloWorld by ST Expansion SW Team

Example of HTTP and HTTPS connections using X-NUCLEO-IDW01M1 Wi-Fi expansion board.
The application is meant to be used with mbed OS 2 ("Classic") only (no mbedOS 5 support).
For HTTPS connection it uses the TLS/SSL feature provided natively by the Wi-Fi module and performs secure connection to the server also verifying the server identity.
To avoid expired CA certificates, system time (in epoch) must be manually entered (e..g. using http://www.epochconverter.com/ ) .
Retrieval of current time from an NTP server is shown by this example.

Committer:
mapellil
Date:
Tue Nov 08 17:23:18 2016 +0000
Revision:
5:526c0df8cbff
Parent:
3:6e7a93483c12
Child:
8:1b5c28fe5e94
fixed tests

Who changed what in which revision?

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