Skip to content

Commit c3023b2

Browse files
committed
Add optional content type, content length and body parameters to post, put, and startRequest
Also, flush client RX data in start request, if state is ready body.
1 parent 4ab54b0 commit c3023b2

File tree

7 files changed

+134
-53
lines changed

7 files changed

+134
-53
lines changed

HttpClient.cpp

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ void HttpClient::beginRequest()
5959
iState = eRequestStarted;
6060
}
6161

62-
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
62+
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod,
63+
const char* aContentType, int aContentLength, const byte aBody[])
6364
{
64-
tHttpState initialState = iState;
65-
66-
if (!iConnectionClose)
65+
if (iState == eReadingBody)
6766
{
6867
flushClientRx();
6968

7069
resetState();
7170
}
7271

72+
tHttpState initialState = iState;
73+
7374
if ((eIdle != iState) && (eRequestStarted != iState))
7475
{
7576
return HTTP_ERROR_API;
@@ -104,12 +105,31 @@ int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
104105

105106
// Now we're connected, send the first part of the request
106107
int ret = sendInitialHeaders(aURLPath, aHttpMethod);
107-
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
108+
109+
if (HTTP_SUCCESS == ret)
108110
{
109-
// This was a simple version of the API, so terminate the headers now
110-
finishHeaders();
111+
if (aContentType)
112+
{
113+
sendHeader(HTTP_HEADER_CONTENT_TYPE, aContentType);
114+
}
115+
116+
if (aContentLength > 0)
117+
{
118+
sendHeader(HTTP_HEADER_CONTENT_LENGTH, aContentLength);
119+
}
120+
121+
if ((initialState == eIdle))
122+
{
123+
// This was a simple version of the API, so terminate the headers now
124+
finishHeaders();
125+
126+
if (aBody && aContentLength > 0)
127+
{
128+
write(aBody, aContentLength);
129+
}
130+
}
131+
// else we'll call it in endRequest or in the first call to print, etc.
111132
}
112-
// else we'll call it in endRequest or in the first call to print, etc.
113133

114134
return ret;
115135
}
@@ -231,12 +251,9 @@ void HttpClient::finishHeaders()
231251

232252
void HttpClient::flushClientRx()
233253
{
234-
if (iClient->connected())
254+
while (iClient->available())
235255
{
236-
while (iClient->available())
237-
{
238-
iClient->read();
239-
}
256+
iClient->read();
240257
}
241258
}
242259

@@ -250,6 +267,66 @@ void HttpClient::endRequest()
250267
// else the end of headers has already been sent, so nothing to do here
251268
}
252269

270+
int HttpClient::get(const char* aURLPath)
271+
{
272+
return startRequest(aURLPath, HTTP_METHOD_GET);
273+
}
274+
275+
int HttpClient::get(const String& aURLPath)
276+
{
277+
return get(aURLPath.c_str());
278+
}
279+
280+
int HttpClient::post(const char* aURLPath)
281+
{
282+
return startRequest(aURLPath, HTTP_METHOD_POST);
283+
}
284+
285+
int HttpClient::post(const String& aURLPath)
286+
{
287+
return post(aURLPath.c_str());
288+
}
289+
290+
int HttpClient::post(const char* aURLPath, const char* aContentType, const char* aBody)
291+
{
292+
return post(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
293+
}
294+
295+
int HttpClient::post(const String& aURLPath, const String& aContentType, const String& aBody)
296+
{
297+
return post(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
298+
}
299+
300+
int HttpClient::post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
301+
{
302+
return startRequest(aURLPath, HTTP_METHOD_POST, aContentType, aContentLength, aBody);
303+
}
304+
305+
int HttpClient::put(const char* aURLPath)
306+
{
307+
return startRequest(aURLPath, HTTP_METHOD_PUT);
308+
}
309+
310+
int HttpClient::put(const String& aURLPath)
311+
{
312+
return put(aURLPath.c_str());
313+
}
314+
315+
int HttpClient::put(const char* aURLPath, const char* aContentType, const char* aBody)
316+
{
317+
return put(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
318+
}
319+
320+
int HttpClient::put(const String& aURLPath, const String& aContentType, const String& aBody)
321+
{
322+
return put(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
323+
}
324+
325+
int HttpClient::put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
326+
{
327+
return startRequest(aURLPath, HTTP_METHOD_PUT, aContentType, aContentLength, aBody);
328+
}
329+
253330
int HttpClient::responseStatusCode()
254331
{
255332
if (iState < eRequestSent)

HttpClient.h

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static const int HTTP_ERROR_INVALID_RESPONSE =-4;
3131
#define HTTP_METHOD_PUT "PUT"
3232
#define HTTP_METHOD_DELETE "DELETE"
3333
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
34+
#define HTTP_HEADER_CONTENT_TYPE "Content-Type"
3435
#define HTTP_HEADER_CONNECTION "Connection"
3536
#define HTTP_HEADER_USER_AGENT "User-Agent"
3637

@@ -64,39 +65,58 @@ class HttpClient : public Client
6465
@param aURLPath Url to request
6566
@return 0 if successful, else error
6667
*/
67-
int get(const char* aURLPath)
68-
{ return startRequest(aURLPath, HTTP_METHOD_GET); }
69-
70-
int get(const String& aURLPath)
71-
{ return get(aURLPath.c_str()); }
68+
int get(const char* aURLPath);
69+
int get(const String& aURLPath);
7270

7371
/** Connect to the server and start to send a POST request.
7472
@param aURLPath Url to request
7573
@return 0 if successful, else error
7674
*/
77-
int post(const char* aURLPath)
78-
{ return startRequest(aURLPath, HTTP_METHOD_POST); }
75+
int post(const char* aURLPath);
76+
int post(const String& aURLPath);
7977

80-
int post(const String& aURLPath)
81-
{ return post(aURLPath.c_str()); }
78+
/** Connect to the server and start to send a POST request
79+
with body and content type
80+
@param aURLPath Url to request
81+
@param aContentType Content type of request body
82+
@param aBody Body of the request
83+
@return 0 if successful, else error
84+
*/
85+
int post(const char* aURLPath, const char* aContentType, const char* aBody);
86+
int post(const String& aURLPath, const String& aContentType, const String& aBody);
87+
int post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
8288

8389
/** Connect to the server and start to send a PUT request.
8490
@param aURLPath Url to request
8591
@return 0 if successful, else error
8692
*/
87-
int put(const char* aURLPath)
88-
{ return startRequest(aURLPath, HTTP_METHOD_PUT); }
93+
int put(const char* aURLPath);
94+
int put(const String& aURLPath);
8995

90-
int put(const String& aURLPath)
91-
{ return put(aURLPath.c_str()); }
96+
/** Connect to the server and start to send a PUT request
97+
with body and content type
98+
@param aURLPath Url to request
99+
@param aContentType Content type of request body
100+
@param aBody Body of the request
101+
@return 0 if successful, else error
102+
*/
103+
int put(const char* aURLPath, const char* aContentType, const char* aBody);
104+
int put(const String& aURLPath, const String& aContentType, const String& aBody);
105+
int put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
92106

93107
/** Connect to the server and start to send the request.
94-
@param aURLPath Url to request
95-
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
108+
@param aURLPath Url to request
109+
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
110+
@param aContentType Content type of request body (optional)
111+
@param aContentLength Length of request body (optional)
112+
@param aBody Body of request (optional)
96113
@return 0 if successful, else error
97114
*/
98115
int startRequest(const char* aURLPath,
99-
const char* aHttpMethod);
116+
const char* aHttpMethod,
117+
const char* aContentType = NULL,
118+
int aContentLength = -1,
119+
const byte aBody[] = NULL);
100120

101121
/** Send an additional header line. This can only be called in between the
102122
calls to startRequest and finishRequest.

examples/DweetGet/DweetGet.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ void loop() {
6363

6464
// send the GET request
6565
Serial.println("making GET request");
66-
client.beginRequest();
6766
client.get(path);
68-
client.endRequest();
6967

7068
// read the status code of the response
7169
statusCode = client.responseStatusCode();

examples/DweetPost/DweetPost.ino

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ void loop() {
5656
String dweetName = "scandalous-cheese-hoarder";
5757
String path = "/dweet/for/" + dweetName;
5858

59+
String contentType = "application/json";
60+
5961
// assemble the body of the POST message:
6062
int sensorValue = analogRead(A0);
6163
String postData = "{\"sensorValue\":\"";
@@ -65,12 +67,7 @@ void loop() {
6567
Serial.println("making POST request");
6668

6769
// send the POST request
68-
client.beginRequest();
69-
client.post(path);
70-
client.sendHeader("Content-Type", "application/json");
71-
client.sendHeader("Content-Length", postData.length());
72-
client.endRequest();
73-
client.write((const byte*)postData.c_str(), postData.length());
70+
client.post(path, contentType, postData);
7471

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

examples/SimpleGet/SimpleGet.ino

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ void setup() {
4949

5050
void loop() {
5151
Serial.println("making GET request");
52-
53-
// read the status code and content length of the response
54-
client.beginRequest();
5552
client.get("/");
56-
client.endRequest();
5753

54+
// read the status code and content length of the response
5855
statusCode = client.responseStatusCode();
5956
contentLength = client.contentLength();
6057

examples/SimplePost/SimplePost.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 POST request");
53+
String contentType = "application/x-www-form-urlencoded";
5354
String postData = "name=Alice&age=12";
5455

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

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

examples/SimplePut/SimplePut.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 PUT request");
53+
String contentType = "application/x-www-form-urlencoded";
5354
String putData = "name=light&age=46";
5455

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

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

0 commit comments

Comments
 (0)