Update library (02 Feb 2015)
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
Fork of HTTPClient_HelloWorld by
main.cpp@6:b6f680d83b3c, 2015-02-01 (annotated)
- Committer:
- ban4jp
- Date:
- Sun Feb 01 15:43:37 2015 +0000
- Revision:
- 6:b6f680d83b3c
- Parent:
- 5:f2c6eeb33c97
Cleanup code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:0e0debc29569 | 1 | #include "mbed.h" |
donatien | 0:0e0debc29569 | 2 | #include "EthernetInterface.h" |
donatien | 0:0e0debc29569 | 3 | #include "HTTPClient.h" |
donatien | 0:0e0debc29569 | 4 | |
donatien | 1:d263603373ac | 5 | EthernetInterface eth; |
donatien | 1:d263603373ac | 6 | HTTPClient http; |
donatien | 1:d263603373ac | 7 | char str[512]; |
donatien | 1:d263603373ac | 8 | |
ban4jp | 5:f2c6eeb33c97 | 9 | int main() |
donatien | 0:0e0debc29569 | 10 | { |
ban4jp | 3:242eb9cf2b1e | 11 | int ret = eth.init(); //Use DHCP |
ban4jp | 5:f2c6eeb33c97 | 12 | if (!ret) { |
ban4jp | 5:f2c6eeb33c97 | 13 | printf("Initialized, MAC: %s\n", eth.getMACAddress()); |
ban4jp | 5:f2c6eeb33c97 | 14 | } else { |
ban4jp | 5:f2c6eeb33c97 | 15 | printf("Error eth.init() - ret = %d\n", ret); |
ban4jp | 5:f2c6eeb33c97 | 16 | return -1; |
ban4jp | 3:242eb9cf2b1e | 17 | } |
donatien | 0:0e0debc29569 | 18 | |
ban4jp | 3:242eb9cf2b1e | 19 | ret = eth.connect(); |
ban4jp | 5:f2c6eeb33c97 | 20 | if (!ret) { |
ban4jp | 5:f2c6eeb33c97 | 21 | printf("Connected, IP: %s, MASK: %s, GW: %s\n", |
ban4jp | 5:f2c6eeb33c97 | 22 | eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
ban4jp | 5:f2c6eeb33c97 | 23 | } else { |
ban4jp | 5:f2c6eeb33c97 | 24 | printf("Error eth.connect() - ret = %d\n", ret); |
ban4jp | 5:f2c6eeb33c97 | 25 | return -1; |
ban4jp | 3:242eb9cf2b1e | 26 | } |
ban4jp | 5:f2c6eeb33c97 | 27 | |
ban4jp | 5:f2c6eeb33c97 | 28 | |
ban4jp | 6:b6f680d83b3c | 29 | // GET data |
ban4jp | 6:b6f680d83b3c | 30 | { |
ban4jp | 6:b6f680d83b3c | 31 | printf("\nTrying to GET request...\n"); |
ban4jp | 6:b6f680d83b3c | 32 | ret = http.get("http://developer.mbed.org/media/uploads/donatien/hello.txt", str, sizeof(str)); |
ban4jp | 6:b6f680d83b3c | 33 | if (!ret) { |
ban4jp | 6:b6f680d83b3c | 34 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
ban4jp | 6:b6f680d83b3c | 35 | printf("Result: %s\n", str); |
ban4jp | 6:b6f680d83b3c | 36 | } else { |
ban4jp | 6:b6f680d83b3c | 37 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
ban4jp | 6:b6f680d83b3c | 38 | } |
ban4jp | 6:b6f680d83b3c | 39 | } |
ban4jp | 6:b6f680d83b3c | 40 | |
ban4jp | 6:b6f680d83b3c | 41 | // POST data |
ban4jp | 6:b6f680d83b3c | 42 | { |
ban4jp | 6:b6f680d83b3c | 43 | HTTPMap map; |
ban4jp | 6:b6f680d83b3c | 44 | HTTPText inText(str, sizeof(str)); |
ban4jp | 6:b6f680d83b3c | 45 | map.put("Hello", "World"); |
ban4jp | 6:b6f680d83b3c | 46 | map.put("test", "1234"); |
ban4jp | 6:b6f680d83b3c | 47 | |
ban4jp | 6:b6f680d83b3c | 48 | printf("\nTrying to POST request...\n"); |
ban4jp | 6:b6f680d83b3c | 49 | ret = http.post("http://httpbin.org/post", map, &inText); |
ban4jp | 6:b6f680d83b3c | 50 | if (!ret) { |
ban4jp | 6:b6f680d83b3c | 51 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
ban4jp | 6:b6f680d83b3c | 52 | printf("Result: %s\n", str); |
ban4jp | 6:b6f680d83b3c | 53 | } else { |
ban4jp | 6:b6f680d83b3c | 54 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
ban4jp | 6:b6f680d83b3c | 55 | } |
donatien | 0:0e0debc29569 | 56 | } |
ban4jp | 5:f2c6eeb33c97 | 57 | |
ban4jp | 6:b6f680d83b3c | 58 | // PUT data |
ban4jp | 6:b6f680d83b3c | 59 | { |
ban4jp | 6:b6f680d83b3c | 60 | strcpy(str, "This is a PUT test!"); |
ban4jp | 6:b6f680d83b3c | 61 | HTTPText outText(str); |
ban4jp | 6:b6f680d83b3c | 62 | HTTPText inText(str, sizeof(str)); |
ban4jp | 6:b6f680d83b3c | 63 | |
ban4jp | 6:b6f680d83b3c | 64 | printf("\nTrying to PUT request...\n"); |
ban4jp | 6:b6f680d83b3c | 65 | ret = http.put("http://httpbin.org/put", outText, &inText); |
ban4jp | 6:b6f680d83b3c | 66 | if (!ret) { |
ban4jp | 6:b6f680d83b3c | 67 | printf("Executed PUT successfully - read %d characters\n", strlen(str)); |
ban4jp | 6:b6f680d83b3c | 68 | printf("Result: %s\n", str); |
ban4jp | 6:b6f680d83b3c | 69 | } else { |
ban4jp | 6:b6f680d83b3c | 70 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
ban4jp | 6:b6f680d83b3c | 71 | } |
donatien | 0:0e0debc29569 | 72 | } |
ban4jp | 5:f2c6eeb33c97 | 73 | |
ban4jp | 6:b6f680d83b3c | 74 | // DELETE data |
ban4jp | 6:b6f680d83b3c | 75 | { |
ban4jp | 6:b6f680d83b3c | 76 | HTTPText inText(str, sizeof(str)); |
ban4jp | 5:f2c6eeb33c97 | 77 | |
ban4jp | 6:b6f680d83b3c | 78 | printf("\nTrying to DELETE request...\n"); |
ban4jp | 6:b6f680d83b3c | 79 | ret = http.del("http://httpbin.org/delete", &inText); |
ban4jp | 6:b6f680d83b3c | 80 | if (!ret) { |
ban4jp | 6:b6f680d83b3c | 81 | printf("Executed DELETE successfully - read %d characters\n", strlen(str)); |
ban4jp | 6:b6f680d83b3c | 82 | printf("Result: %s\n", str); |
ban4jp | 6:b6f680d83b3c | 83 | } else { |
ban4jp | 6:b6f680d83b3c | 84 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
ban4jp | 6:b6f680d83b3c | 85 | } |
donatien | 2:270e2d0bb85a | 86 | } |
ban4jp | 5:f2c6eeb33c97 | 87 | |
ban4jp | 5:f2c6eeb33c97 | 88 | |
ban4jp | 3:242eb9cf2b1e | 89 | printf("\n"); |
ban4jp | 6:b6f680d83b3c | 90 | |
ban4jp | 5:f2c6eeb33c97 | 91 | ret = eth.disconnect(); |
ban4jp | 5:f2c6eeb33c97 | 92 | if (!ret) { |
ban4jp | 5:f2c6eeb33c97 | 93 | printf("Disconnected\n"); |
ban4jp | 5:f2c6eeb33c97 | 94 | } else { |
ban4jp | 5:f2c6eeb33c97 | 95 | printf("Error eth.disconnect() - ret = %d\n", ret); |
ban4jp | 3:242eb9cf2b1e | 96 | } |
ban4jp | 3:242eb9cf2b1e | 97 | |
donatien | 0:0e0debc29569 | 98 | |
donatien | 0:0e0debc29569 | 99 | while(1) { |
donatien | 0:0e0debc29569 | 100 | } |
donatien | 0:0e0debc29569 | 101 | } |