From 8f42a68a507e55b79bb72b3a823cf693709fee1a Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 22 Jun 2016 12:59:38 -0400 Subject: [PATCH] Always finish headers if a request body is provided to startRequest --- HttpClient.cpp | 14 ++++++++------ HttpClient.h | 7 ++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/HttpClient.cpp b/HttpClient.cpp index e131272..4159b9f 100644 --- a/HttpClient.cpp +++ b/HttpClient.cpp @@ -118,17 +118,19 @@ int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod, sendHeader(HTTP_HEADER_CONTENT_LENGTH, aContentLength); } - if ((initialState == eIdle)) + bool hasBody = (aBody && aContentLength > 0); + + if (initialState == eIdle || hasBody) { // This was a simple version of the API, so terminate the headers now finishHeaders(); - - if (aBody && aContentLength > 0) - { - write(aBody, aContentLength); - } } // else we'll call it in endRequest or in the first call to print, etc. + + if (hasBody) + { + write(aBody, aContentLength); + } } return ret; diff --git a/HttpClient.h b/HttpClient.h index d5633cd..141b2df 100644 --- a/HttpClient.h +++ b/HttpClient.h @@ -75,7 +75,7 @@ public: int post(const char* aURLPath); int post(const String& aURLPath); - /** Connect to the server and start to send a POST request + /** Connect to the server and send a POST request with body and content type @param aURLPath Url to request @param aContentType Content type of request body @@ -93,7 +93,7 @@ public: int put(const char* aURLPath); int put(const String& aURLPath); - /** Connect to the server and start to send a PUT request + /** Connect to the server and send a PUT request with body and content type @param aURLPath Url to request @param aContentType Content type of request body @@ -111,7 +111,7 @@ public: int del(const char* aURLPath); int del(const String& aURLPath); - /** Connect to the server and start to send a DELETE request + /** Connect to the server and send a DELETE request with body and content type @param aURLPath Url to request @param aContentType Content type of request body @@ -123,6 +123,7 @@ public: int del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]); /** Connect to the server and start to send the request. + If a body is provided, the entire request (including headers and body) will be sent @param aURLPath Url to request @param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc. @param aContentType Content type of request body (optional)