diff --git a/HttpClient.cpp b/HttpClient.cpp index 72c471f..8545aaa 100644 --- a/HttpClient.cpp +++ b/HttpClient.cpp @@ -327,6 +327,31 @@ int HttpClient::put(const char* aURLPath, const char* aContentType, int aContent return startRequest(aURLPath, HTTP_METHOD_PUT, aContentType, aContentLength, aBody); } +int HttpClient::del(const char* aURLPath) +{ + return startRequest(aURLPath, HTTP_METHOD_DELETE); +} + +int HttpClient::del(const String& aURLPath) +{ + return del(aURLPath.c_str()); +} + +int HttpClient::del(const char* aURLPath, const char* aContentType, const char* aBody) +{ + return del(aURLPath, aContentType, strlen(aBody), (const byte*)aBody); +} + +int HttpClient::del(const String& aURLPath, const String& aContentType, const String& aBody) +{ + return del(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str()); +} + +int HttpClient::del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]) +{ + return startRequest(aURLPath, HTTP_METHOD_DELETE, aContentType, aContentLength, aBody); +} + int HttpClient::responseStatusCode() { if (iState < eRequestSent) diff --git a/HttpClient.h b/HttpClient.h index 06af847..639d7a5 100644 --- a/HttpClient.h +++ b/HttpClient.h @@ -104,6 +104,24 @@ public: int put(const String& aURLPath, const String& aContentType, const String& aBody); int put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]); + /** Connect to the server and start to send a DELETE request. + @param aURLPath Url to request + @return 0 if successful, else error + */ + int del(const char* aURLPath); + int del(const String& aURLPath); + + /** Connect to the server and start to send a DELETE request + with body and content type + @param aURLPath Url to request + @param aContentType Content type of request body + @param aBody Body of the request + @return 0 if successful, else error + */ + int del(const char* aURLPath, const char* aContentType, const char* aBody); + int del(const String& aURLPath, const String& aContentType, const String& aBody); + int del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]); + /** Connect to the server and start to send the request. @param aURLPath Url to request @param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc. diff --git a/examples/SimpleDelete/SimpleDelete.ino b/examples/SimpleDelete/SimpleDelete.ino index 591f4de..787a6fe 100644 --- a/examples/SimpleDelete/SimpleDelete.ino +++ b/examples/SimpleDelete/SimpleDelete.ino @@ -50,14 +50,10 @@ void setup() { void loop() { Serial.println("making DELETE request"); + String contentType = "application/x-www-form-urlencoded"; String delData = "name=light&age=46"; - client.beginRequest(); - client.startRequest("/", HTTP_METHOD_DELETE); - client.sendHeader("Content-Type", "application/x-www-form-urlencoded"); - client.sendHeader("Content-Length", delData.length()); - client.endRequest(); - client.write((const byte*)delData.c_str(), delData.length()); + client.del("/", contentType, delData); // read the status code and content length of the response statusCode = client.responseStatusCode();