mirror of
https://github.com/arduino-libraries/ArduinoHttpClient.git
synced 2025-06-07 07:42:01 +03:00
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.
This commit is contained in:
parent
4ab54b0a1a
commit
c3023b25ee
@ -59,17 +59,18 @@ void HttpClient::beginRequest()
|
|||||||
iState = eRequestStarted;
|
iState = eRequestStarted;
|
||||||
}
|
}
|
||||||
|
|
||||||
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
|
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod,
|
||||||
|
const char* aContentType, int aContentLength, const byte aBody[])
|
||||||
{
|
{
|
||||||
tHttpState initialState = iState;
|
if (iState == eReadingBody)
|
||||||
|
|
||||||
if (!iConnectionClose)
|
|
||||||
{
|
{
|
||||||
flushClientRx();
|
flushClientRx();
|
||||||
|
|
||||||
resetState();
|
resetState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tHttpState initialState = iState;
|
||||||
|
|
||||||
if ((eIdle != iState) && (eRequestStarted != iState))
|
if ((eIdle != iState) && (eRequestStarted != iState))
|
||||||
{
|
{
|
||||||
return HTTP_ERROR_API;
|
return HTTP_ERROR_API;
|
||||||
@ -104,12 +105,31 @@ int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
|
|||||||
|
|
||||||
// Now we're connected, send the first part of the request
|
// Now we're connected, send the first part of the request
|
||||||
int ret = sendInitialHeaders(aURLPath, aHttpMethod);
|
int ret = sendInitialHeaders(aURLPath, aHttpMethod);
|
||||||
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
|
|
||||||
|
if (HTTP_SUCCESS == ret)
|
||||||
|
{
|
||||||
|
if (aContentType)
|
||||||
|
{
|
||||||
|
sendHeader(HTTP_HEADER_CONTENT_TYPE, aContentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aContentLength > 0)
|
||||||
|
{
|
||||||
|
sendHeader(HTTP_HEADER_CONTENT_LENGTH, aContentLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((initialState == eIdle))
|
||||||
{
|
{
|
||||||
// This was a simple version of the API, so terminate the headers now
|
// This was a simple version of the API, so terminate the headers now
|
||||||
finishHeaders();
|
finishHeaders();
|
||||||
|
|
||||||
|
if (aBody && aContentLength > 0)
|
||||||
|
{
|
||||||
|
write(aBody, aContentLength);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// else we'll call it in endRequest or in the first call to print, etc.
|
// else we'll call it in endRequest or in the first call to print, etc.
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -230,15 +250,12 @@ void HttpClient::finishHeaders()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HttpClient::flushClientRx()
|
void HttpClient::flushClientRx()
|
||||||
{
|
|
||||||
if (iClient->connected())
|
|
||||||
{
|
{
|
||||||
while (iClient->available())
|
while (iClient->available())
|
||||||
{
|
{
|
||||||
iClient->read();
|
iClient->read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void HttpClient::endRequest()
|
void HttpClient::endRequest()
|
||||||
{
|
{
|
||||||
@ -250,6 +267,66 @@ void HttpClient::endRequest()
|
|||||||
// else the end of headers has already been sent, so nothing to do here
|
// else the end of headers has already been sent, so nothing to do here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int HttpClient::get(const char* aURLPath)
|
||||||
|
{
|
||||||
|
return startRequest(aURLPath, HTTP_METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::get(const String& aURLPath)
|
||||||
|
{
|
||||||
|
return get(aURLPath.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::post(const char* aURLPath)
|
||||||
|
{
|
||||||
|
return startRequest(aURLPath, HTTP_METHOD_POST);
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::post(const String& aURLPath)
|
||||||
|
{
|
||||||
|
return post(aURLPath.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::post(const char* aURLPath, const char* aContentType, const char* aBody)
|
||||||
|
{
|
||||||
|
return post(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::post(const String& aURLPath, const String& aContentType, const String& aBody)
|
||||||
|
{
|
||||||
|
return post(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
|
||||||
|
{
|
||||||
|
return startRequest(aURLPath, HTTP_METHOD_POST, aContentType, aContentLength, aBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::put(const char* aURLPath)
|
||||||
|
{
|
||||||
|
return startRequest(aURLPath, HTTP_METHOD_PUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::put(const String& aURLPath)
|
||||||
|
{
|
||||||
|
return put(aURLPath.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::put(const char* aURLPath, const char* aContentType, const char* aBody)
|
||||||
|
{
|
||||||
|
return put(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::put(const String& aURLPath, const String& aContentType, const String& aBody)
|
||||||
|
{
|
||||||
|
return put(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
|
||||||
|
{
|
||||||
|
return startRequest(aURLPath, HTTP_METHOD_PUT, aContentType, aContentLength, aBody);
|
||||||
|
}
|
||||||
|
|
||||||
int HttpClient::responseStatusCode()
|
int HttpClient::responseStatusCode()
|
||||||
{
|
{
|
||||||
if (iState < eRequestSent)
|
if (iState < eRequestSent)
|
||||||
|
48
HttpClient.h
48
HttpClient.h
@ -31,6 +31,7 @@ static const int HTTP_ERROR_INVALID_RESPONSE =-4;
|
|||||||
#define HTTP_METHOD_PUT "PUT"
|
#define HTTP_METHOD_PUT "PUT"
|
||||||
#define HTTP_METHOD_DELETE "DELETE"
|
#define HTTP_METHOD_DELETE "DELETE"
|
||||||
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
|
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
|
||||||
|
#define HTTP_HEADER_CONTENT_TYPE "Content-Type"
|
||||||
#define HTTP_HEADER_CONNECTION "Connection"
|
#define HTTP_HEADER_CONNECTION "Connection"
|
||||||
#define HTTP_HEADER_USER_AGENT "User-Agent"
|
#define HTTP_HEADER_USER_AGENT "User-Agent"
|
||||||
|
|
||||||
@ -64,39 +65,58 @@ public:
|
|||||||
@param aURLPath Url to request
|
@param aURLPath Url to request
|
||||||
@return 0 if successful, else error
|
@return 0 if successful, else error
|
||||||
*/
|
*/
|
||||||
int get(const char* aURLPath)
|
int get(const char* aURLPath);
|
||||||
{ return startRequest(aURLPath, HTTP_METHOD_GET); }
|
int get(const String& aURLPath);
|
||||||
|
|
||||||
int get(const String& aURLPath)
|
|
||||||
{ return get(aURLPath.c_str()); }
|
|
||||||
|
|
||||||
/** Connect to the server and start to send a POST request.
|
/** Connect to the server and start to send a POST request.
|
||||||
@param aURLPath Url to request
|
@param aURLPath Url to request
|
||||||
@return 0 if successful, else error
|
@return 0 if successful, else error
|
||||||
*/
|
*/
|
||||||
int post(const char* aURLPath)
|
int post(const char* aURLPath);
|
||||||
{ return startRequest(aURLPath, HTTP_METHOD_POST); }
|
int post(const String& aURLPath);
|
||||||
|
|
||||||
int post(const String& aURLPath)
|
/** Connect to the server and start to send a POST request
|
||||||
{ return post(aURLPath.c_str()); }
|
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 post(const char* aURLPath, const char* aContentType, const char* aBody);
|
||||||
|
int post(const String& aURLPath, const String& aContentType, const String& aBody);
|
||||||
|
int post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
|
||||||
|
|
||||||
/** Connect to the server and start to send a PUT request.
|
/** Connect to the server and start to send a PUT request.
|
||||||
@param aURLPath Url to request
|
@param aURLPath Url to request
|
||||||
@return 0 if successful, else error
|
@return 0 if successful, else error
|
||||||
*/
|
*/
|
||||||
int put(const char* aURLPath)
|
int put(const char* aURLPath);
|
||||||
{ return startRequest(aURLPath, HTTP_METHOD_PUT); }
|
int put(const String& aURLPath);
|
||||||
|
|
||||||
int put(const String& aURLPath)
|
/** Connect to the server and start to send a PUT request
|
||||||
{ return put(aURLPath.c_str()); }
|
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 put(const char* aURLPath, const char* aContentType, const char* aBody);
|
||||||
|
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 the request.
|
/** Connect to the server and start to send the request.
|
||||||
@param aURLPath Url to request
|
@param aURLPath Url to request
|
||||||
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
|
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
|
||||||
|
@param aContentType Content type of request body (optional)
|
||||||
|
@param aContentLength Length of request body (optional)
|
||||||
|
@param aBody Body of request (optional)
|
||||||
@return 0 if successful, else error
|
@return 0 if successful, else error
|
||||||
*/
|
*/
|
||||||
int startRequest(const char* aURLPath,
|
int startRequest(const char* aURLPath,
|
||||||
const char* aHttpMethod);
|
const char* aHttpMethod,
|
||||||
|
const char* aContentType = NULL,
|
||||||
|
int aContentLength = -1,
|
||||||
|
const byte aBody[] = NULL);
|
||||||
|
|
||||||
/** Send an additional header line. This can only be called in between the
|
/** Send an additional header line. This can only be called in between the
|
||||||
calls to startRequest and finishRequest.
|
calls to startRequest and finishRequest.
|
||||||
|
@ -63,9 +63,7 @@ void loop() {
|
|||||||
|
|
||||||
// send the GET request
|
// send the GET request
|
||||||
Serial.println("making GET request");
|
Serial.println("making GET request");
|
||||||
client.beginRequest();
|
|
||||||
client.get(path);
|
client.get(path);
|
||||||
client.endRequest();
|
|
||||||
|
|
||||||
// read the status code of the response
|
// read the status code of the response
|
||||||
statusCode = client.responseStatusCode();
|
statusCode = client.responseStatusCode();
|
||||||
|
@ -56,6 +56,8 @@ void loop() {
|
|||||||
String dweetName = "scandalous-cheese-hoarder";
|
String dweetName = "scandalous-cheese-hoarder";
|
||||||
String path = "/dweet/for/" + dweetName;
|
String path = "/dweet/for/" + dweetName;
|
||||||
|
|
||||||
|
String contentType = "application/json";
|
||||||
|
|
||||||
// assemble the body of the POST message:
|
// assemble the body of the POST message:
|
||||||
int sensorValue = analogRead(A0);
|
int sensorValue = analogRead(A0);
|
||||||
String postData = "{\"sensorValue\":\"";
|
String postData = "{\"sensorValue\":\"";
|
||||||
@ -65,12 +67,7 @@ void loop() {
|
|||||||
Serial.println("making POST request");
|
Serial.println("making POST request");
|
||||||
|
|
||||||
// send the POST request
|
// send the POST request
|
||||||
client.beginRequest();
|
client.post(path, contentType, postData);
|
||||||
client.post(path);
|
|
||||||
client.sendHeader("Content-Type", "application/json");
|
|
||||||
client.sendHeader("Content-Length", postData.length());
|
|
||||||
client.endRequest();
|
|
||||||
client.write((const byte*)postData.c_str(), postData.length());
|
|
||||||
|
|
||||||
// read the status code and content length of the response
|
// read the status code and content length of the response
|
||||||
statusCode = client.responseStatusCode();
|
statusCode = client.responseStatusCode();
|
||||||
|
@ -49,12 +49,9 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Serial.println("making GET request");
|
Serial.println("making GET request");
|
||||||
|
client.get("/");
|
||||||
|
|
||||||
// read the status code and content length of the response
|
// read the status code and content length of the response
|
||||||
client.beginRequest();
|
|
||||||
client.get("/");
|
|
||||||
client.endRequest();
|
|
||||||
|
|
||||||
statusCode = client.responseStatusCode();
|
statusCode = client.responseStatusCode();
|
||||||
contentLength = client.contentLength();
|
contentLength = client.contentLength();
|
||||||
|
|
||||||
|
@ -50,14 +50,10 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Serial.println("making POST request");
|
Serial.println("making POST request");
|
||||||
|
String contentType = "application/x-www-form-urlencoded";
|
||||||
String postData = "name=Alice&age=12";
|
String postData = "name=Alice&age=12";
|
||||||
|
|
||||||
client.beginRequest();
|
client.post("/", contentType, postData);
|
||||||
client.post("/");
|
|
||||||
client.sendHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
||||||
client.sendHeader("Content-Length", postData.length());
|
|
||||||
client.endRequest();
|
|
||||||
client.write((const byte*)postData.c_str(), postData.length());
|
|
||||||
|
|
||||||
// read the status code and content length of the response
|
// read the status code and content length of the response
|
||||||
statusCode = client.responseStatusCode();
|
statusCode = client.responseStatusCode();
|
||||||
|
@ -50,14 +50,10 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Serial.println("making PUT request");
|
Serial.println("making PUT request");
|
||||||
|
String contentType = "application/x-www-form-urlencoded";
|
||||||
String putData = "name=light&age=46";
|
String putData = "name=light&age=46";
|
||||||
|
|
||||||
client.beginRequest();
|
client.put("/", contentType, putData);
|
||||||
client.put("/");
|
|
||||||
client.sendHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
||||||
client.sendHeader("Content-Length", putData.length());
|
|
||||||
client.endRequest();
|
|
||||||
client.write((const byte*)putData.c_str(), putData.length());
|
|
||||||
|
|
||||||
// read the status code and content length of the response
|
// read the status code and content length of the response
|
||||||
statusCode = client.responseStatusCode();
|
statusCode = client.responseStatusCode();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user