Attempting to POST data with HttpClient.
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
Fork of HTTPClient_HelloWorld by
main.cpp@3:64b3aa4b90f1, 2015-03-18 (annotated)
- Committer:
- terrence
- Date:
- Wed Mar 18 11:23:08 2015 +0000
- Revision:
- 3:64b3aa4b90f1
- Parent:
- 2:270e2d0bb85a
Can't set headers.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terrence | 3:64b3aa4b90f1 | 1 | //find httpclient-ssl by mason. |
donatien | 0:0e0debc29569 | 2 | #include "mbed.h" |
donatien | 0:0e0debc29569 | 3 | #include "EthernetInterface.h" |
donatien | 0:0e0debc29569 | 4 | #include "HTTPClient.h" |
terrence | 3:64b3aa4b90f1 | 5 | |
donatien | 1:d263603373ac | 6 | EthernetInterface eth; |
donatien | 1:d263603373ac | 7 | HTTPClient http; |
donatien | 1:d263603373ac | 8 | char str[512]; |
terrence | 3:64b3aa4b90f1 | 9 | char json[125]; |
donatien | 1:d263603373ac | 10 | |
donatien | 0:0e0debc29569 | 11 | int main() |
donatien | 0:0e0debc29569 | 12 | { |
terrence | 3:64b3aa4b90f1 | 13 | // HTTPText text("text/html", bufsiz); |
terrence | 3:64b3aa4b90f1 | 14 | //HttpText(const string& encoding = "text/html") |
terrence | 3:64b3aa4b90f1 | 15 | |
terrence | 3:64b3aa4b90f1 | 16 | // printf("(Buffer size of a HTTPText : %d)\n", bufsiz); |
terrence | 3:64b3aa4b90f1 | 17 | // HTTPResult r = client.get(url, &text); |
terrence | 3:64b3aa4b90f1 | 18 | //'{\"Email\":\"tps@tps.com\",\"PhoneNumber\":\"512-244-6688\",\"RowKey\":\"20141122:105633\",\"PartitionKey\":\"spencer\"}' |
terrence | 3:64b3aa4b90f1 | 19 | //strcpy(str, "This is a PUT test!"); |
terrence | 3:64b3aa4b90f1 | 20 | //HTTPText outText(str); |
donatien | 0:0e0debc29569 | 21 | |
terrence | 3:64b3aa4b90f1 | 22 | |
donatien | 0:0e0debc29569 | 23 | |
terrence | 3:64b3aa4b90f1 | 24 | eth.init(); //Use DHCP |
terrence | 3:64b3aa4b90f1 | 25 | eth.connect(); |
terrence | 3:64b3aa4b90f1 | 26 | printf("IP Address is %s\n", eth.getIPAddress()); |
terrence | 3:64b3aa4b90f1 | 27 | |
terrence | 3:64b3aa4b90f1 | 28 | //POST data |
terrence | 3:64b3aa4b90f1 | 29 | HTTPMap map; |
terrence | 3:64b3aa4b90f1 | 30 | map.put("value", "World"); |
terrence | 3:64b3aa4b90f1 | 31 | |
terrence | 3:64b3aa4b90f1 | 32 | http.setDataType("Content-Type","application/x-www-form-urlencoded"); |
terrence | 3:64b3aa4b90f1 | 33 | http.setRequestHeader("Content-Length",210); |
terrence | 3:64b3aa4b90f1 | 34 | string data = "={\"PartitionKey\":\"LSlaughter\",\"RowKey\":\"20150318:111818\",\"SubLocationID\":\"600W8thOffice\"}"; |
terrence | 3:64b3aa4b90f1 | 35 | HTTPText jsonText("value"); |
terrence | 3:64b3aa4b90f1 | 36 | jsonText.set(data); |
terrence | 3:64b3aa4b90f1 | 37 | printf("\nTrying to post data...\r\n"); |
terrence | 3:64b3aa4b90f1 | 38 | |
terrence | 3:64b3aa4b90f1 | 39 | int ret = http.post("http://tpswebapi1.azurewebsites.net/api/values", jsonText, null); |
terrence | 3:64b3aa4b90f1 | 40 | |
donatien | 0:0e0debc29569 | 41 | if (!ret) |
donatien | 0:0e0debc29569 | 42 | { |
terrence | 3:64b3aa4b90f1 | 43 | printf("Executed POST successfully - read %d characters\r\n", strlen(str)); |
terrence | 3:64b3aa4b90f1 | 44 | printf("Result: %s\r\n", str); |
donatien | 0:0e0debc29569 | 45 | } |
donatien | 0:0e0debc29569 | 46 | else |
donatien | 0:0e0debc29569 | 47 | { |
terrence | 3:64b3aa4b90f1 | 48 | printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
donatien | 0:0e0debc29569 | 49 | } |
donatien | 0:0e0debc29569 | 50 | |
terrence | 3:64b3aa4b90f1 | 51 | |
donatien | 0:0e0debc29569 | 52 | eth.disconnect(); |
terrence | 3:64b3aa4b90f1 | 53 | |
donatien | 0:0e0debc29569 | 54 | while(1) { |
donatien | 0:0e0debc29569 | 55 | } |
terrence | 3:64b3aa4b90f1 | 56 | } |