mirror of
https://github.com/arduino-libraries/ArduinoHttpClient.git
synced 2025-04-19 21:22:15 +03:00
Add new responseBody API to simplify reading response body as a String
This commit is contained in:
parent
bdc5281733
commit
f56eecbc6f
@ -509,6 +509,24 @@ int HttpClient::contentLength()
|
||||
return iContentLength;
|
||||
}
|
||||
|
||||
String HttpClient::responseBody()
|
||||
{
|
||||
int bodyLength = contentLength();
|
||||
String response;
|
||||
|
||||
if (bodyLength > 0)
|
||||
{
|
||||
response.reserve(bodyLength);
|
||||
}
|
||||
|
||||
while (available())
|
||||
{
|
||||
response += (char)read();
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
bool HttpClient::endOfBodyReached()
|
||||
{
|
||||
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))
|
||||
|
@ -245,6 +245,13 @@ public:
|
||||
*/
|
||||
int contentLength();
|
||||
|
||||
/** Return the response body as a String
|
||||
Also skips response headers if they have not been read already
|
||||
MUST be called after responseStatusCode()
|
||||
@return response body of request as a String
|
||||
*/
|
||||
String responseBody();
|
||||
|
||||
/** Enables connection keep-alive mode
|
||||
*/
|
||||
void connectionKeepAlive();
|
||||
|
@ -33,7 +33,6 @@ WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
int statusCode = 0;
|
||||
int contentLength = 0;
|
||||
String response;
|
||||
|
||||
void setup() {
|
||||
@ -65,23 +64,11 @@ void loop() {
|
||||
Serial.println("making GET request");
|
||||
client.get(path);
|
||||
|
||||
// read the status code of the response
|
||||
// read the status code and body of the response
|
||||
statusCode = client.responseStatusCode();
|
||||
response = client.responseBody();
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
|
||||
// read the content length of the response
|
||||
contentLength = client.contentLength();
|
||||
Serial.print("Content Length: ");
|
||||
Serial.println(contentLength);
|
||||
|
||||
// read the response body
|
||||
response = "";
|
||||
response.reserve(contentLength);
|
||||
while (client.available()) {
|
||||
response += (char)client.read();
|
||||
}
|
||||
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
|
@ -27,7 +27,6 @@ WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
int statusCode = 0;
|
||||
int contentLength = 0;
|
||||
String response;
|
||||
|
||||
void setup() {
|
||||
@ -69,16 +68,9 @@ void loop() {
|
||||
// send the POST request
|
||||
client.post(path, contentType, postData);
|
||||
|
||||
// read the status code and content length of the response
|
||||
// read the status code and body of the response
|
||||
statusCode = client.responseStatusCode();
|
||||
contentLength = client.contentLength();
|
||||
|
||||
// read the response body
|
||||
response = "";
|
||||
response.reserve(contentLength);
|
||||
while (client.available()) {
|
||||
response += (char)client.read();
|
||||
}
|
||||
response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
|
@ -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: ");
|
||||
|
@ -26,7 +26,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
String response;
|
||||
int statusCode = 0;
|
||||
int contentLength = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
@ -55,16 +54,9 @@ void loop() {
|
||||
|
||||
client.del("/", contentType, delData);
|
||||
|
||||
// read the status code and content length of the response
|
||||
// read the status code and body of the response
|
||||
statusCode = client.responseStatusCode();
|
||||
contentLength = client.contentLength();
|
||||
|
||||
// read the response body
|
||||
response = "";
|
||||
response.reserve(contentLength);
|
||||
while (client.available()) {
|
||||
response += (char)client.read();
|
||||
}
|
||||
response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
|
@ -25,7 +25,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
String response;
|
||||
int statusCode = 0;
|
||||
int contentLength = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
@ -51,16 +50,9 @@ void loop() {
|
||||
Serial.println("making GET request");
|
||||
client.get("/");
|
||||
|
||||
// read the status code and content length of the response
|
||||
// read the status code and body of the response
|
||||
statusCode = client.responseStatusCode();
|
||||
contentLength = client.contentLength();
|
||||
|
||||
// read the response body
|
||||
response = "";
|
||||
response.reserve(contentLength);
|
||||
while (client.available()) {
|
||||
response += (char)client.read();
|
||||
}
|
||||
response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
|
@ -26,7 +26,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
String response;
|
||||
int statusCode = 0;
|
||||
int contentLength = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
@ -55,16 +54,9 @@ void loop() {
|
||||
|
||||
client.post("/", contentType, postData);
|
||||
|
||||
// read the status code and content length of the response
|
||||
// read the status code and body of the response
|
||||
statusCode = client.responseStatusCode();
|
||||
contentLength = client.contentLength();
|
||||
|
||||
// read the response body
|
||||
response = "";
|
||||
response.reserve(contentLength);
|
||||
while (client.available()) {
|
||||
response += (char)client.read();
|
||||
}
|
||||
response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
|
@ -26,7 +26,6 @@ HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
String response;
|
||||
int statusCode = 0;
|
||||
int contentLength = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
@ -55,16 +54,9 @@ void loop() {
|
||||
|
||||
client.put("/", contentType, putData);
|
||||
|
||||
// read the status code and content length of the response
|
||||
// read the status code and body of the response
|
||||
statusCode = client.responseStatusCode();
|
||||
contentLength = client.contentLength();
|
||||
|
||||
// read the response body
|
||||
response = "";
|
||||
response.reserve(contentLength);
|
||||
while (client.available()) {
|
||||
response += (char)client.read();
|
||||
}
|
||||
response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
|
@ -33,6 +33,7 @@ noDefaultRequestHeaders KEYWORD2
|
||||
headerAvailable KEYWORD2
|
||||
readHeaderName KEYWORD2
|
||||
readHeaderValue KEYWORD2
|
||||
responseBody KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user