1
0
mirror of https://github.com/arduino-libraries/ArduinoHttpClient.git synced 2025-06-11 17:08:08 +03:00

Add new responseBody API to simplify reading response body as a String

This commit is contained in:
Sandeep Mistry
2016-06-22 12:47:56 -04:00
parent bdc5281733
commit f56eecbc6f
10 changed files with 43 additions and 80 deletions

View File

@ -70,6 +70,8 @@ void sendRequest(int light, String cmd, String value) {
request += light;
request += "/state/";
String contentType = "application/json";
// make a string for the JSON command:
String hueCmd = "{\"" + cmd;
hueCmd += "\":";
@ -81,23 +83,11 @@ void sendRequest(int light, String cmd, String value) {
Serial.print("JSON command to server: ");
// make the PUT request to the hub:
httpClient.beginRequest();
httpClient.put(request);
httpClient.sendHeader("Content-Type", "application/json");
httpClient.sendHeader("Content-Length", hueCmd.length());
httpClient.endRequest();
httpClient.write((const byte*)hueCmd.c_str(), hueCmd.length());
httpClient.put(request, contentType, hueCmd);
// read the status code and content length of the response
// read the status code and body of the response
int statusCode = httpClient.responseStatusCode();
int contentLength = httpClient.contentLength();
// read the response body
String response = "";
response.reserve(contentLength);
while (httpClient.available()) {
response += (char)httpClient.read();
}
String response = httpClient.responseBody();
Serial.println(hueCmd);
Serial.print("Status code from server: ");