http client example

Dependencies:   C027_Support HTTPClient mbed

Fork of HTTPClient_HelloWorld by Donatien Garnier

Committer:
mazgch
Date:
Tue May 27 09:17:49 2014 +0000
Revision:
6:6ff6061a0f76
Parent:
5:a18ddbfd70c9
Child:
7:b14d0f112a73
pull latest lib (update API)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:0e0debc29569 1 #include "mbed.h"
donatien 0:0e0debc29569 2 #include "HTTPClient.h"
mazgch 6:6ff6061a0f76 3
mazgch 6:6ff6061a0f76 4 //------------------------------------------------------------------------------------
mazgch 6:6ff6061a0f76 5 // You need to configure these cellular modem / SIM parameters.
mazgch 6:6ff6061a0f76 6 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 6:6ff6061a0f76 7 //------------------------------------------------------------------------------------
mazgch 3:412a526d7054 8 #include "MDM.h"
mazgch 6:6ff6061a0f76 9 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 3:412a526d7054 10 #define SIMPIN NULL
mazgch 6:6ff6061a0f76 11 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 6:6ff6061a0f76 12 contract with the network operator. You can also try to look-up your settings in
mazgch 6:6ff6061a0f76 13 google: https://www.google.de/search?q=APN+list */
mazgch 3:412a526d7054 14 #define APN "gprs.swisscom.ch"
mazgch 6:6ff6061a0f76 15 //! Set the user name for your APN, or NULL if not needed
mazgch 3:412a526d7054 16 #define USERNAME NULL
mazgch 6:6ff6061a0f76 17 //! Set the password for your APN, or NULL if not needed
mazgch 3:412a526d7054 18 #define PASSWORD NULL
mazgch 6:6ff6061a0f76 19 //------------------------------------------------------------------------------------
mazgch 3:412a526d7054 20
donatien 1:d263603373ac 21 char str[512];
donatien 1:d263603373ac 22
donatien 0:0e0debc29569 23 int main()
donatien 0:0e0debc29569 24 {
mazgch 5:a18ddbfd70c9 25 // turn on the supplies of the Modem
mazgch 3:412a526d7054 26 MDMSerial mdm;
mazgch 6:6ff6061a0f76 27 //mdm.setDebug(4); // enable this for debugging issues
mazgch 6:6ff6061a0f76 28 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 4:7fd97087e573 29 return -1;
donatien 1:d263603373ac 30
mazgch 4:7fd97087e573 31 HTTPClient http;
mazgch 4:7fd97087e573 32
mazgch 4:7fd97087e573 33 //GET data
mazgch 4:7fd97087e573 34 printf("\nTrying to fetch page...\n");
mazgch 4:7fd97087e573 35 int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
mazgch 4:7fd97087e573 36 if (!ret)
donatien 0:0e0debc29569 37 {
mazgch 4:7fd97087e573 38 printf("Page fetched successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 39 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 40 }
mazgch 4:7fd97087e573 41 else
mazgch 4:7fd97087e573 42 {
mazgch 4:7fd97087e573 43 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 4:7fd97087e573 44 }
donatien 0:0e0debc29569 45
mazgch 4:7fd97087e573 46 //POST data
mazgch 4:7fd97087e573 47 HTTPMap map;
mazgch 4:7fd97087e573 48 HTTPText inText(str, 512);
mazgch 4:7fd97087e573 49 map.put("Hello", "World");
mazgch 4:7fd97087e573 50 map.put("test", "1234");
mazgch 4:7fd97087e573 51 printf("\nTrying to post data...\n");
mazgch 4:7fd97087e573 52 ret = http.post("http://httpbin.org/post", map, &inText);
mazgch 4:7fd97087e573 53 if (!ret)
mazgch 4:7fd97087e573 54 {
mazgch 4:7fd97087e573 55 printf("Executed POST successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 56 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 57 }
mazgch 4:7fd97087e573 58 else
mazgch 4:7fd97087e573 59 {
mazgch 4:7fd97087e573 60 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 4:7fd97087e573 61 }
donatien 2:270e2d0bb85a 62
mazgch 4:7fd97087e573 63 //PUT data
mazgch 4:7fd97087e573 64 strcpy(str, "This is a PUT test!");
mazgch 4:7fd97087e573 65 HTTPText outText(str);
mazgch 4:7fd97087e573 66 //HTTPText inText(str, 512);
mazgch 4:7fd97087e573 67 printf("\nTrying to put resource...\n");
mazgch 4:7fd97087e573 68 ret = http.put("http://httpbin.org/put", outText, &inText);
mazgch 4:7fd97087e573 69 if (!ret)
mazgch 4:7fd97087e573 70 {
mazgch 4:7fd97087e573 71 printf("Executed PUT successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 72 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 73 }
mazgch 4:7fd97087e573 74 else
mazgch 4:7fd97087e573 75 {
mazgch 4:7fd97087e573 76 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
donatien 2:270e2d0bb85a 77 }
mazgch 4:7fd97087e573 78
mazgch 4:7fd97087e573 79 //DELETE data
mazgch 4:7fd97087e573 80 //HTTPText inText(str, 512);
mazgch 4:7fd97087e573 81 printf("\nTrying to delete resource...\n");
mazgch 4:7fd97087e573 82 ret = http.del("http://httpbin.org/delete", &inText);
mazgch 4:7fd97087e573 83 if (!ret)
mazgch 4:7fd97087e573 84 {
mazgch 4:7fd97087e573 85 printf("Executed DELETE successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 86 printf("Result: %s\n", str);
mazgch 4:7fd97087e573 87 }
mazgch 4:7fd97087e573 88 else
mazgch 4:7fd97087e573 89 {
mazgch 4:7fd97087e573 90 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
mazgch 4:7fd97087e573 91 }
mazgch 4:7fd97087e573 92
mazgch 4:7fd97087e573 93 mdm.disconnect();
mazgch 3:412a526d7054 94 mdm.powerOff();
mazgch 6:6ff6061a0f76 95
mazgch 3:412a526d7054 96 while(true);
donatien 0:0e0debc29569 97 }