http client example
Dependencies: C027_Support HTTPClient mbed
Fork of HTTPClient_HelloWorld by
main.cpp@5:a18ddbfd70c9, 2014-05-13 (annotated)
- Committer:
- mazgch
- Date:
- Tue May 13 09:34:52 2014 +0000
- Revision:
- 5:a18ddbfd70c9
- Parent:
- 4:7fd97087e573
- Child:
- 6:6ff6061a0f76
update libs
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 "HTTPClient.h" |
mazgch | 3:412a526d7054 | 3 | #include "C027.h" |
mazgch | 3:412a526d7054 | 4 | #include "MDM.h" |
donatien | 0:0e0debc29569 | 5 | |
mazgch | 3:412a526d7054 | 6 | //---------------------------------------------------------------------- |
mazgch | 3:412a526d7054 | 7 | // You may need to configure these parameters |
mazgch | 3:412a526d7054 | 8 | |
mazgch | 3:412a526d7054 | 9 | /** Set your secret SIM pin here "1234" |
mazgch | 3:412a526d7054 | 10 | */ |
mazgch | 3:412a526d7054 | 11 | #define SIMPIN NULL |
mazgch | 3:412a526d7054 | 12 | |
mazgch | 3:412a526d7054 | 13 | /** The APN of your network operator, sometimes it is "internet" |
mazgch | 3:412a526d7054 | 14 | check your contract with the network operator |
mazgch | 3:412a526d7054 | 15 | */ |
mazgch | 3:412a526d7054 | 16 | #define APN "gprs.swisscom.ch" |
mazgch | 3:412a526d7054 | 17 | |
mazgch | 3:412a526d7054 | 18 | /** Set the user name for your APN, or NULL if not needed |
mazgch | 3:412a526d7054 | 19 | */ |
mazgch | 3:412a526d7054 | 20 | #define USERNAME NULL |
mazgch | 3:412a526d7054 | 21 | |
mazgch | 3:412a526d7054 | 22 | /** Set the password for your APN, or NULL if not needed |
mazgch | 3:412a526d7054 | 23 | */ |
mazgch | 3:412a526d7054 | 24 | #define PASSWORD NULL |
mazgch | 3:412a526d7054 | 25 | |
mazgch | 3:412a526d7054 | 26 | C027 c027; |
mazgch | 3:412a526d7054 | 27 | |
donatien | 1:d263603373ac | 28 | char str[512]; |
donatien | 1:d263603373ac | 29 | |
donatien | 0:0e0debc29569 | 30 | int main() |
donatien | 0:0e0debc29569 | 31 | { |
mazgch | 5:a18ddbfd70c9 | 32 | // turn on the supplies of the Modem |
mazgch | 3:412a526d7054 | 33 | c027.mdmPower(true); |
mazgch | 4:7fd97087e573 | 34 | printf("Modem Initialize\r\n"); |
mazgch | 3:412a526d7054 | 35 | MDMSerial mdm; |
mazgch | 4:7fd97087e573 | 36 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true)) |
mazgch | 4:7fd97087e573 | 37 | return -1; |
donatien | 1:d263603373ac | 38 | |
mazgch | 4:7fd97087e573 | 39 | HTTPClient http; |
mazgch | 4:7fd97087e573 | 40 | |
mazgch | 4:7fd97087e573 | 41 | //GET data |
mazgch | 4:7fd97087e573 | 42 | printf("\nTrying to fetch page...\n"); |
mazgch | 4:7fd97087e573 | 43 | int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
mazgch | 4:7fd97087e573 | 44 | if (!ret) |
donatien | 0:0e0debc29569 | 45 | { |
mazgch | 4:7fd97087e573 | 46 | printf("Page fetched successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 47 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 48 | } |
mazgch | 4:7fd97087e573 | 49 | else |
mazgch | 4:7fd97087e573 | 50 | { |
mazgch | 4:7fd97087e573 | 51 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
mazgch | 4:7fd97087e573 | 52 | } |
donatien | 0:0e0debc29569 | 53 | |
mazgch | 4:7fd97087e573 | 54 | //POST data |
mazgch | 4:7fd97087e573 | 55 | HTTPMap map; |
mazgch | 4:7fd97087e573 | 56 | HTTPText inText(str, 512); |
mazgch | 4:7fd97087e573 | 57 | map.put("Hello", "World"); |
mazgch | 4:7fd97087e573 | 58 | map.put("test", "1234"); |
mazgch | 4:7fd97087e573 | 59 | printf("\nTrying to post data...\n"); |
mazgch | 4:7fd97087e573 | 60 | ret = http.post("http://httpbin.org/post", map, &inText); |
mazgch | 4:7fd97087e573 | 61 | if (!ret) |
mazgch | 4:7fd97087e573 | 62 | { |
mazgch | 4:7fd97087e573 | 63 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 64 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 65 | } |
mazgch | 4:7fd97087e573 | 66 | else |
mazgch | 4:7fd97087e573 | 67 | { |
mazgch | 4:7fd97087e573 | 68 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
mazgch | 4:7fd97087e573 | 69 | } |
donatien | 2:270e2d0bb85a | 70 | |
mazgch | 4:7fd97087e573 | 71 | //PUT data |
mazgch | 4:7fd97087e573 | 72 | strcpy(str, "This is a PUT test!"); |
mazgch | 4:7fd97087e573 | 73 | HTTPText outText(str); |
mazgch | 4:7fd97087e573 | 74 | //HTTPText inText(str, 512); |
mazgch | 4:7fd97087e573 | 75 | printf("\nTrying to put resource...\n"); |
mazgch | 4:7fd97087e573 | 76 | ret = http.put("http://httpbin.org/put", outText, &inText); |
mazgch | 4:7fd97087e573 | 77 | if (!ret) |
mazgch | 4:7fd97087e573 | 78 | { |
mazgch | 4:7fd97087e573 | 79 | printf("Executed PUT successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 80 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 81 | } |
mazgch | 4:7fd97087e573 | 82 | else |
mazgch | 4:7fd97087e573 | 83 | { |
mazgch | 4:7fd97087e573 | 84 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
donatien | 2:270e2d0bb85a | 85 | } |
mazgch | 4:7fd97087e573 | 86 | |
mazgch | 4:7fd97087e573 | 87 | //DELETE data |
mazgch | 4:7fd97087e573 | 88 | //HTTPText inText(str, 512); |
mazgch | 4:7fd97087e573 | 89 | printf("\nTrying to delete resource...\n"); |
mazgch | 4:7fd97087e573 | 90 | ret = http.del("http://httpbin.org/delete", &inText); |
mazgch | 4:7fd97087e573 | 91 | if (!ret) |
mazgch | 4:7fd97087e573 | 92 | { |
mazgch | 4:7fd97087e573 | 93 | printf("Executed DELETE successfully - read %d characters\n", strlen(str)); |
mazgch | 4:7fd97087e573 | 94 | printf("Result: %s\n", str); |
mazgch | 4:7fd97087e573 | 95 | } |
mazgch | 4:7fd97087e573 | 96 | else |
mazgch | 4:7fd97087e573 | 97 | { |
mazgch | 4:7fd97087e573 | 98 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
mazgch | 4:7fd97087e573 | 99 | } |
mazgch | 4:7fd97087e573 | 100 | |
mazgch | 4:7fd97087e573 | 101 | mdm.disconnect(); |
mazgch | 3:412a526d7054 | 102 | mdm.powerOff(); |
mazgch | 3:412a526d7054 | 103 | c027.mdmPower(false); |
mazgch | 4:7fd97087e573 | 104 | printf("Done\n"); |
donatien | 2:270e2d0bb85a | 105 | |
mazgch | 3:412a526d7054 | 106 | while(true); |
donatien | 0:0e0debc29569 | 107 | } |