Skip to content

Commit bdc5281

Browse files
committed
Add new del API for HTTP DELETE method
1 parent c3023b2 commit bdc5281

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

HttpClient.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,31 @@ int HttpClient::put(const char* aURLPath, const char* aContentType, int aContent
327327
return startRequest(aURLPath, HTTP_METHOD_PUT, aContentType, aContentLength, aBody);
328328
}
329329

330+
int HttpClient::del(const char* aURLPath)
331+
{
332+
return startRequest(aURLPath, HTTP_METHOD_DELETE);
333+
}
334+
335+
int HttpClient::del(const String& aURLPath)
336+
{
337+
return del(aURLPath.c_str());
338+
}
339+
340+
int HttpClient::del(const char* aURLPath, const char* aContentType, const char* aBody)
341+
{
342+
return del(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
343+
}
344+
345+
int HttpClient::del(const String& aURLPath, const String& aContentType, const String& aBody)
346+
{
347+
return del(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
348+
}
349+
350+
int HttpClient::del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
351+
{
352+
return startRequest(aURLPath, HTTP_METHOD_DELETE, aContentType, aContentLength, aBody);
353+
}
354+
330355
int HttpClient::responseStatusCode()
331356
{
332357
if (iState < eRequestSent)

HttpClient.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ class HttpClient : public Client
104104
int put(const String& aURLPath, const String& aContentType, const String& aBody);
105105
int put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
106106

107+
/** Connect to the server and start to send a DELETE request.
108+
@param aURLPath Url to request
109+
@return 0 if successful, else error
110+
*/
111+
int del(const char* aURLPath);
112+
int del(const String& aURLPath);
113+
114+
/** Connect to the server and start to send a DELETE request
115+
with body and content type
116+
@param aURLPath Url to request
117+
@param aContentType Content type of request body
118+
@param aBody Body of the request
119+
@return 0 if successful, else error
120+
*/
121+
int del(const char* aURLPath, const char* aContentType, const char* aBody);
122+
int del(const String& aURLPath, const String& aContentType, const String& aBody);
123+
int del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
124+
107125
/** Connect to the server and start to send the request.
108126
@param aURLPath Url to request
109127
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.

examples/SimpleDelete/SimpleDelete.ino

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@ void setup() {
5050

5151
void loop() {
5252
Serial.println("making DELETE request");
53+
String contentType = "application/x-www-form-urlencoded";
5354
String delData = "name=light&age=46";
5455

55-
client.beginRequest();
56-
client.startRequest("/", HTTP_METHOD_DELETE);
57-
client.sendHeader("Content-Type", "application/x-www-form-urlencoded");
58-
client.sendHeader("Content-Length", delData.length());
59-
client.endRequest();
60-
client.write((const byte*)delData.c_str(), delData.length());
56+
client.del("/", contentType, delData);
6157

6258
// read the status code and content length of the response
6359
statusCode = client.responseStatusCode();

0 commit comments

Comments
 (0)