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
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.
Diff: main.cpp
- Revision:
- 0:0e0debc29569
- Child:
- 1:d263603373ac
diff -r 000000000000 -r 0e0debc29569 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jun 29 11:07:21 2012 +0000 @@ -0,0 +1,48 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "HTTPClient.h" + +int main() +{ + EthernetInterface eth; + eth.init(); //Use DHCP + + eth.connect(); + + //GET data + HTTPClient http; + char str[512]; + printf("Trying to fetch page...\n"); + int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); + if (!ret) + { + printf("Page fetched successfully - read %d characters\n", strlen(str)); + printf("Result: %s\n", str); + } + else + { + printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + } + + //POST data + HTTPMap map; + HTTPText text(str, 512); + map.put("Hello", "World"); + map.put("test", "1234"); + printf("Trying to post data...\n"); + ret = http.post("http://httpbin.org/post", map, &text); + if (!ret) + { + printf("Executed POST successfully - read %d characters\n", strlen(str)); + printf("Result: %s\n", str); + } + else + { + printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + } + + eth.disconnect(); + + while(1) { + } +}