This is a quick example of a simple HTTP client program using the network-socket API that is provided as a part of mbed-os. The program brings up an underlying network interface, and uses it to perform an HTTP transaction over a TCPSocket.
Revision 40:afef93b6d854, committed 2018-03-27
- Comitter:
- mbed_official
- Date:
- Tue Mar 27 12:45:08 2018 +0100
- Parent:
- 39:6c688e7f0c05
- Child:
- 41:8dc0efa46bf7
- Commit message:
- Merge branch 'mbed-os-5.8.0-oob'
* mbed-os-5.8.0-oob:
Updating mbed-os to mbed-os-5.8.0-rc2
Read until connection is properly closed.
Add build instructions.
Convert line-feeds for stdout.
Updating mbed-os to mbed-os-5.8.0-rc1
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets
Changed in this revision
--- a/README.md Tue Mar 20 10:00:03 2018 +0000 +++ b/README.md Tue Mar 27 12:45:08 2018 +0100 @@ -10,10 +10,18 @@ To use the example with a different interface, you will need to modify main.cpp and replace the EthernetInterface class with the appropriate interface. -**Note:** The default serial port baud rate is 9600 bit/s. +### Building + +``` +mbed compile -t <toolchain> -m <target> +``` + +For example, building for K64F using GCC: `mbed compile -t GCC_ARM -m K64F` ### Expected output ### +**Note:** The default serial port baud rate is 9600 bit/s. + ``` IP address: 10.118.14.45 Netmask: 255.255.252.0
--- a/main.cpp Tue Mar 20 10:00:03 2018 +0000 +++ b/main.cpp Tue Mar 27 12:45:08 2018 +0100 @@ -25,12 +25,20 @@ char *buffer = new char[256]; // Send an HTTP request - strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n"); + strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n"); int scount = socket.send(buffer, strlen(buffer)); printf("sent %d [%.*s]\n", scount, strstr(buffer, "\r\n")-buffer, buffer); // Recieve an HTTP response and print out the response line - int rcount = socket.recv(buffer, 256); + int received = 0; + int remaining = 256; + int rcount = 0; + char *p = buffer; + while (0 != (received = socket.recv(p, remaining))) { + p += received; + rcount += received; + remaining -= received; + } printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer); // The api.ipify.org service also gives us the device's external IP address
--- a/mbed-os.lib Tue Mar 20 10:00:03 2018 +0000 +++ b/mbed-os.lib Tue Mar 27 12:45:08 2018 +0100 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#16bac101a6b7b4724023dcf86ece1548e3a23cbf +https://github.com/ARMmbed/mbed-os/#ad284b28064b22d7e125bcd83e3b5bf649bc4c37
--- a/mbed_app.json Tue Mar 20 10:00:03 2018 +0000 +++ b/mbed_app.json Tue Mar 27 12:45:08 2018 +0100 @@ -1,5 +1,8 @@ { "target_overrides": { + "*": { + "platform.stdio-convert-newlines": true + }, "UBLOX_EVK_ODIN_W2": { "target.device_has_remove": ["EMAC"] }