Skip to content

Commit f56eecb

Browse files
committed
Add new responseBody API to simplify reading response body as a String
1 parent bdc5281 commit f56eecb

File tree

10 files changed

+43
-80
lines changed

10 files changed

+43
-80
lines changed

HttpClient.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,24 @@ int HttpClient::contentLength()
509509
return iContentLength;
510510
}
511511

512+
String HttpClient::responseBody()
513+
{
514+
int bodyLength = contentLength();
515+
String response;
516+
517+
if (bodyLength > 0)
518+
{
519+
response.reserve(bodyLength);
520+
}
521+
522+
while (available())
523+
{
524+
response += (char)read();
525+
}
526+
527+
return response;
528+
}
529+
512530
bool HttpClient::endOfBodyReached()
513531
{
514532
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))

HttpClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ class HttpClient : public Client
245245
*/
246246
int contentLength();
247247

248+
/** Return the response body as a String
249+
Also skips response headers if they have not been read already
250+
MUST be called after responseStatusCode()
251+
@return response body of request as a String
252+
*/
253+
String responseBody();
254+
248255
/** Enables connection keep-alive mode
249256
*/
250257
void connectionKeepAlive();

examples/DweetGet/DweetGet.ino

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ WiFiClient wifi;
3333
HttpClient client = HttpClient(wifi, serverAddress, port);
3434
int status = WL_IDLE_STATUS;
3535
int statusCode = 0;
36-
int contentLength = 0;
3736
String response;
3837

3938
void setup() {
@@ -65,23 +64,11 @@ void loop() {
6564
Serial.println("making GET request");
6665
client.get(path);
6766

68-
// read the status code of the response
67+
// read the status code and body of the response
6968
statusCode = client.responseStatusCode();
69+
response = client.responseBody();
7070
Serial.print("Status code: ");
7171
Serial.println(statusCode);
72-
73-
// read the content length of the response
74-
contentLength = client.contentLength();
75-
Serial.print("Content Length: ");
76-
Serial.println(contentLength);
77-
78-
// read the response body
79-
response = "";
80-
response.reserve(contentLength);
81-
while (client.available()) {
82-
response += (char)client.read();
83-
}
84-
8572
Serial.print("Response: ");
8673
Serial.println(response);
8774

examples/DweetPost/DweetPost.ino

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ WiFiClient wifi;
2727
HttpClient client = HttpClient(wifi, serverAddress, port);
2828
int status = WL_IDLE_STATUS;
2929
int statusCode = 0;
30-
int contentLength = 0;
3130
String response;
3231

3332
void setup() {
@@ -69,16 +68,9 @@ void loop() {
6968
// send the POST request
7069
client.post(path, contentType, postData);
7170

72-
// read the status code and content length of the response
71+
// read the status code and body of the response
7372
statusCode = client.responseStatusCode();
74-
contentLength = client.contentLength();
75-
76-
// read the response body
77-
response = "";
78-
response.reserve(contentLength);
79-
while (client.available()) {
80-
response += (char)client.read();
81-
}
73+
response = client.responseBody();
8274

8375
Serial.print("Status code: ");
8476
Serial.println(statusCode);

examples/HueBlink/HueBlink.ino

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ void sendRequest(int light, String cmd, String value) {
7070
request += light;
7171
request += "/state/";
7272

73+
String contentType = "application/json";
74+
7375
// make a string for the JSON command:
7476
String hueCmd = "{\"" + cmd;
7577
hueCmd += "\":";
@@ -81,23 +83,11 @@ void sendRequest(int light, String cmd, String value) {
8183
Serial.print("JSON command to server: ");
8284

8385
// make the PUT request to the hub:
84-
httpClient.beginRequest();
85-
httpClient.put(request);
86-
httpClient.sendHeader("Content-Type", "application/json");
87-
httpClient.sendHeader("Content-Length", hueCmd.length());
88-
httpClient.endRequest();
89-
httpClient.write((const byte*)hueCmd.c_str(), hueCmd.length());
86+
httpClient.put(request, contentType, hueCmd);
9087

91-
// read the status code and content length of the response
88+
// read the status code and body of the response
9289
int statusCode = httpClient.responseStatusCode();
93-
int contentLength = httpClient.contentLength();
94-
95-
// read the response body
96-
String response = "";
97-
response.reserve(contentLength);
98-
while (httpClient.available()) {
99-
response += (char)httpClient.read();
100-
}
90+
String response = httpClient.responseBody();
10191

10292
Serial.println(hueCmd);
10393
Serial.print("Status code from server: ");

examples/SimpleDelete/SimpleDelete.ino

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
2626
int status = WL_IDLE_STATUS;
2727
String response;
2828
int statusCode = 0;
29-
int contentLength = 0;
3029

3130
void setup() {
3231
Serial.begin(9600);
@@ -55,16 +54,9 @@ void loop() {
5554

5655
client.del("/", contentType, delData);
5756

58-
// read the status code and content length of the response
57+
// read the status code and body of the response
5958
statusCode = client.responseStatusCode();
60-
contentLength = client.contentLength();
61-
62-
// read the response body
63-
response = "";
64-
response.reserve(contentLength);
65-
while (client.available()) {
66-
response += (char)client.read();
67-
}
59+
response = client.responseBody();
6860

6961
Serial.print("Status code: ");
7062
Serial.println(statusCode);

examples/SimpleGet/SimpleGet.ino

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
2525
int status = WL_IDLE_STATUS;
2626
String response;
2727
int statusCode = 0;
28-
int contentLength = 0;
2928

3029
void setup() {
3130
Serial.begin(9600);
@@ -51,16 +50,9 @@ void loop() {
5150
Serial.println("making GET request");
5251
client.get("/");
5352

54-
// read the status code and content length of the response
53+
// read the status code and body of the response
5554
statusCode = client.responseStatusCode();
56-
contentLength = client.contentLength();
57-
58-
// read the response body
59-
response = "";
60-
response.reserve(contentLength);
61-
while (client.available()) {
62-
response += (char)client.read();
63-
}
55+
response = client.responseBody();
6456

6557
Serial.print("Status code: ");
6658
Serial.println(statusCode);

examples/SimplePost/SimplePost.ino

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
2626
int status = WL_IDLE_STATUS;
2727
String response;
2828
int statusCode = 0;
29-
int contentLength = 0;
3029

3130
void setup() {
3231
Serial.begin(9600);
@@ -55,16 +54,9 @@ void loop() {
5554

5655
client.post("/", contentType, postData);
5756

58-
// read the status code and content length of the response
57+
// read the status code and body of the response
5958
statusCode = client.responseStatusCode();
60-
contentLength = client.contentLength();
61-
62-
// read the response body
63-
response = "";
64-
response.reserve(contentLength);
65-
while (client.available()) {
66-
response += (char)client.read();
67-
}
59+
response = client.responseBody();
6860

6961
Serial.print("Status code: ");
7062
Serial.println(statusCode);

examples/SimplePut/SimplePut.ino

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
2626
int status = WL_IDLE_STATUS;
2727
String response;
2828
int statusCode = 0;
29-
int contentLength = 0;
3029

3130
void setup() {
3231
Serial.begin(9600);
@@ -55,16 +54,9 @@ void loop() {
5554

5655
client.put("/", contentType, putData);
5756

58-
// read the status code and content length of the response
57+
// read the status code and body of the response
5958
statusCode = client.responseStatusCode();
60-
contentLength = client.contentLength();
61-
62-
// read the response body
63-
response = "";
64-
response.reserve(contentLength);
65-
while (client.available()) {
66-
response += (char)client.read();
67-
}
59+
response = client.responseBody();
6860

6961
Serial.print("Status code: ");
7062
Serial.println(statusCode);

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ noDefaultRequestHeaders KEYWORD2
3333
headerAvailable KEYWORD2
3434
readHeaderName KEYWORD2
3535
readHeaderValue KEYWORD2
36+
responseBody KEYWORD2
3637

3738
#######################################
3839
# Constants (LITERAL1)

0 commit comments

Comments
 (0)