@@ -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
232252void 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+
253330int HttpClient::responseStatusCode ()
254331{
255332 if (iState < eRequestSent)
0 commit comments