![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
Diff: http.cpp
- Revision:
- 2:1cbb20dd1733
- Parent:
- 1:27b35752c5d0
- Child:
- 12:daddfc44a0f5
--- a/http.cpp Tue Nov 18 18:28:52 2014 +0000 +++ b/http.cpp Sun Dec 14 22:28:24 2014 +0000 @@ -2,9 +2,8 @@ #include "http.h" #include <string> #include <vector> -#include <sstream> -http::http() +void http::connect() { #ifdef DEBUG printf("[HTTP] Ethernet connecting...\r\n"); @@ -21,7 +20,7 @@ string http::get(string address, int port, string url, int replyTimeout) { #ifdef DEBUG - printf("[HTTP] Sending GET request to %s%s\r\n", address.c_str(), url.c_str()); + printf("[HTTP] Sending GET request to %s:%i%s\r\n", address.c_str(), port, url.c_str()); #endif TCPSocketConnection sock; @@ -50,7 +49,7 @@ string http::post(string address, int port, string url, string jsonPayload, int replyTimeout) { #ifdef DEBUG - printf("[HTTP] Sending POST request to %s%s\r\n", address.c_str(), url.c_str()); + printf("[HTTP] Sending POST request to %s:%i%s\r\n", address.c_str(), port, url.c_str()); #endif TCPSocketConnection sock; @@ -60,14 +59,15 @@ printf("[HTTP] Connected to endpoint...\r\n"); #endif - stringstream contentLength; - contentLength << jsonPayload.size(); - string contentLengthStr = contentLength.str(); + char buffer[20]; + sprintf(buffer, "%i", jsonPayload.size()); + string contentLengthStr = string(buffer); string httppost = "POST " + url + " HTTP/1.1"; httppost += "\nHost: " + address; httppost += "\nContent-Type: application/json"; httppost += "\nContent-Length: " + contentLengthStr; + httppost += "\nAuthorization: Key 1"; httppost += "\n\n" + jsonPayload; //to get a writable char* (I.E. not const char* returned by string.c_str), put it into a vector @@ -103,5 +103,16 @@ string http::parse(string httpReply) { - return httpReply; + int payloadBegin = httpReply.find("\r\n\r\n"); + + if (payloadBegin > -1) + { + string payload(httpReply.begin() + payloadBegin + 4, httpReply.end()); + + return payload; + } + else + { + return ""; + } } \ No newline at end of file