Skip to content

Commit 6766c10

Browse files
committed
* making HTTPClient::sendRequest can adjust uri when we reuse connection
1 parent 79e5d4c commit 6766c10

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,46 @@ int HTTPClient::sendRequest(const char * type, String payload)
373373
return sendRequest(type, (uint8_t *) payload.c_str(), payload.length());
374374
}
375375

376+
/**
377+
* sendRequest
378+
* @param type const char * "GET", "POST", ....
379+
* @param type const char * "/URI"
380+
* @param payload uint8_t * data for the message body if null not send
381+
* @param size size_t size for the message body if 0 not send
382+
* @return -1 if no info or > 0 when Content-Length is set by server
383+
*/
384+
int HTTPClient::sendRequest(const char * type, uint8_t * uri,uint8_t * payload, size_t size)
385+
{
386+
Serial.printf("[HTTP-Client][sendRequest] switch uri <%s> to <%s>\n",_uri.c_str(),uri);
387+
_uri = String((char *)uri);
388+
389+
// connect to server
390+
if(!connect()) {
391+
return returnError(HTTPC_ERROR_CONNECTION_REFUSED);
392+
}
393+
394+
if(payload && size > 0) {
395+
addHeader(F("Content-Length"), String(size));
396+
}
397+
398+
// send Header
399+
if(!sendHeader(type)) {
400+
return returnError(HTTPC_ERROR_SEND_HEADER_FAILED);
401+
}
402+
403+
// send Payload if needed
404+
if(payload && size > 0) {
405+
if(_tcp->write(&payload[0], size) != size) {
406+
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
407+
}
408+
}
409+
410+
// handle Server Response (Header)
411+
return returnError(handleHeaderResponse());
412+
}
413+
414+
415+
376416
/**
377417
* sendRequest
378418
* @param type const char * "GET", "POST", ....

libraries/HTTPClient/src/HTTPClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class HTTPClient
152152
int sendRequest(const char * type, String payload);
153153
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
154154
int sendRequest(const char * type, Stream * stream, size_t size = 0);
155+
int sendRequest(const char * type, uint8_t * uri,uint8_t * payload, size_t size = 0);
155156

156157
void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
157158

0 commit comments

Comments
 (0)