slovic
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
Fork of Ethernet_UDP_client by
main.cpp@1:2725b204ea40, 2016-03-01 (annotated)
- Committer:
- perodot
- Date:
- Tue Mar 01 15:59:49 2016 +0000
- Revision:
- 1:2725b204ea40
- Parent:
- 0:29858159c001
k64f http
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mareikeFSL | 0:29858159c001 | 1 | #include "mbed.h" |
mareikeFSL | 0:29858159c001 | 2 | #include "EthernetInterface.h" |
perodot | 1:2725b204ea40 | 3 | #include "HTTPClient.h" |
mareikeFSL | 0:29858159c001 | 4 | |
perodot | 1:2725b204ea40 | 5 | EthernetInterface eth; |
perodot | 1:2725b204ea40 | 6 | HTTPClient http; |
perodot | 1:2725b204ea40 | 7 | char str[512]; |
mareikeFSL | 0:29858159c001 | 8 | |
perodot | 1:2725b204ea40 | 9 | int main() |
perodot | 1:2725b204ea40 | 10 | { |
perodot | 1:2725b204ea40 | 11 | eth.init(); //Use DHCP |
mareikeFSL | 0:29858159c001 | 12 | |
perodot | 1:2725b204ea40 | 13 | eth.connect(); |
mareikeFSL | 0:29858159c001 | 14 | |
perodot | 1:2725b204ea40 | 15 | //GET data |
perodot | 1:2725b204ea40 | 16 | printf("\nTrying to fetch page...\n"); |
perodot | 1:2725b204ea40 | 17 | int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
perodot | 1:2725b204ea40 | 18 | if (!ret) { |
perodot | 1:2725b204ea40 | 19 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
perodot | 1:2725b204ea40 | 20 | printf("Result: %s\n", str); |
perodot | 1:2725b204ea40 | 21 | } else { |
perodot | 1:2725b204ea40 | 22 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
perodot | 1:2725b204ea40 | 23 | } |
mareikeFSL | 0:29858159c001 | 24 | |
perodot | 1:2725b204ea40 | 25 | //POST data |
perodot | 1:2725b204ea40 | 26 | HTTPMap map; |
perodot | 1:2725b204ea40 | 27 | HTTPText inText(str, 512); |
perodot | 1:2725b204ea40 | 28 | map.put("Hello", "World"); |
perodot | 1:2725b204ea40 | 29 | map.put("test", "1234"); |
perodot | 1:2725b204ea40 | 30 | printf("\nTrying to post data...\n"); |
perodot | 1:2725b204ea40 | 31 | ret = http.post("http://httpbin.org/post", map, &inText); |
perodot | 1:2725b204ea40 | 32 | if (!ret) { |
perodot | 1:2725b204ea40 | 33 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
perodot | 1:2725b204ea40 | 34 | printf("Result: %s\n", str); |
perodot | 1:2725b204ea40 | 35 | } else { |
perodot | 1:2725b204ea40 | 36 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
perodot | 1:2725b204ea40 | 37 | } |
mareikeFSL | 0:29858159c001 | 38 | |
perodot | 1:2725b204ea40 | 39 | //PUT data |
perodot | 1:2725b204ea40 | 40 | strcpy(str, "This is a PUT test!"); |
perodot | 1:2725b204ea40 | 41 | HTTPText outText(str); |
perodot | 1:2725b204ea40 | 42 | //HTTPText inText(str, 512); |
perodot | 1:2725b204ea40 | 43 | printf("\nTrying to put resource...\n"); |
perodot | 1:2725b204ea40 | 44 | ret = http.put("http://httpbin.org/put", outText, &inText); |
perodot | 1:2725b204ea40 | 45 | if (!ret) { |
perodot | 1:2725b204ea40 | 46 | printf("Executed PUT successfully - read %d characters\n", strlen(str)); |
perodot | 1:2725b204ea40 | 47 | printf("Result: %s\n", str); |
perodot | 1:2725b204ea40 | 48 | } else { |
perodot | 1:2725b204ea40 | 49 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
perodot | 1:2725b204ea40 | 50 | } |
mareikeFSL | 0:29858159c001 | 51 | |
perodot | 1:2725b204ea40 | 52 | //DELETE data |
perodot | 1:2725b204ea40 | 53 | //HTTPText inText(str, 512); |
perodot | 1:2725b204ea40 | 54 | printf("\nTrying to delete resource...\n"); |
perodot | 1:2725b204ea40 | 55 | ret = http.del("http://httpbin.org/delete", &inText); |
perodot | 1:2725b204ea40 | 56 | if (!ret) { |
perodot | 1:2725b204ea40 | 57 | printf("Executed DELETE successfully - read %d characters\n", strlen(str)); |
perodot | 1:2725b204ea40 | 58 | printf("Result: %s\n", str); |
perodot | 1:2725b204ea40 | 59 | } else { |
perodot | 1:2725b204ea40 | 60 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
mareikeFSL | 0:29858159c001 | 61 | } |
perodot | 1:2725b204ea40 | 62 | |
perodot | 1:2725b204ea40 | 63 | eth.disconnect(); |
perodot | 1:2725b204ea40 | 64 | |
perodot | 1:2725b204ea40 | 65 | while(1) { |
perodot | 1:2725b204ea40 | 66 | } |
perodot | 1:2725b204ea40 | 67 | } |