CloudPlugs REST Library  1.0.0
for Arduino
HttpClient Class Reference
Inheritance diagram for HttpClient:

Public Member Functions

 HttpClient (Client &aClient)
 
void beginRequest ()
 
void endRequest ()
 
int get (const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aUserAgent=NULL)
 
int get (const char *aServerName, const char *aURLPath, const char *aUserAgent=NULL)
 
int get (const IPAddress &aServerAddress, const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aUserAgent=NULL)
 
int get (const IPAddress &aServerAddress, const char *aServerName, const char *aURLPath, const char *aUserAgent=NULL)
 
int post (const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aUserAgent=NULL)
 
int post (const char *aServerName, const char *aURLPath, const char *aUserAgent=NULL)
 
int post (const IPAddress &aServerAddress, const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aUserAgent=NULL)
 
int post (const IPAddress &aServerAddress, const char *aServerName, const char *aURLPath, const char *aUserAgent=NULL)
 
int put (const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aUserAgent=NULL)
 
int put (const char *aServerName, const char *aURLPath, const char *aUserAgent=NULL)
 
int put (const IPAddress &aServerAddress, const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aUserAgent=NULL)
 
int put (const IPAddress &aServerAddress, const char *aServerName, const char *aURLPath, const char *aUserAgent=NULL)
 
int startRequest (const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aHttpMethod, const char *aUserAgent)
 
int startRequest (const IPAddress &aServerAddress, const char *aServerName, uint16_t aServerPort, const char *aURLPath, const char *aHttpMethod, const char *aUserAgent)
 
void sendHeader (const char *aHeader)
 
void sendHeader (const char *aHeaderName, const char *aHeaderValue)
 
void sendHeader (const char *aHeaderName, const int aHeaderValue)
 
void sendBasicAuth (const char *aUser, const char *aPassword)
 
void finishRequest ()
 
int responseStatusCode ()
 
int readHeader ()
 
int skipResponseHeaders ()
 
bool endOfHeadersReached ()
 
bool endOfBodyReached ()
 
virtual bool endOfStream ()
 
virtual bool completed ()
 
int contentLength ()
 
virtual size_t write (uint8_t aByte)
 
virtual size_t write (const uint8_t *aBuffer, size_t aSize)
 
virtual int available ()
 
virtual int read ()
 
virtual int read (uint8_t *buf, size_t size)
 
virtual int peek ()
 
virtual void flush ()
 
virtual int connect (IPAddress ip, uint16_t port)
 
virtual int connect (const char *host, uint16_t port)
 
virtual void stop ()
 
virtual uint8_t connected ()
 
virtual operator bool ()
 
virtual uint32_t httpResponseTimeout ()
 
virtual void setHttpResponseTimeout (uint32_t timeout)
 

Static Public Attributes

static const int kNoContentLengthHeader =-1
 
static const uint16_t kHttpPort =80
 
static const char * kUserAgent
 

Protected Types

enum  tHttpState {
  eIdle, eRequestStarted, eRequestSent, eReadingStatusCode,
  eStatusCodeRead, eReadingContentLength, eSkipToEndOfHeader, eLineStartingCRFound,
  eReadingBody
}
 

Protected Member Functions

void resetState ()
 
int sendInitialHeaders (const char *aServerName, IPAddress aServerIP, uint16_t aPort, const char *aURLPath, const char *aHttpMethod, const char *aUserAgent)
 
void finishHeaders ()
 

Protected Attributes

ClientiClient
 
tHttpState iState
 
int iStatusCode
 
int iContentLength
 
int iBodyLengthConsumed
 
const char * iContentLengthPtr
 
IPAddress iProxyAddress
 
uint16_t iProxyPort
 
uint32_t iHttpResponseTimeout
 

Static Protected Attributes

static const int kHttpWaitForDataDelay = 1000
 
static const int kHttpResponseTimeout = 30*1000
 
static const char * kContentLengthPrefix
 

Member Enumeration Documentation

◆ tHttpState

enum tHttpState
protected
Enumerator
eIdle 
eRequestStarted 
eRequestSent 
eReadingStatusCode 
eStatusCodeRead 
eReadingContentLength 
eSkipToEndOfHeader 
eLineStartingCRFound 
eReadingBody 

Constructor & Destructor Documentation

◆ HttpClient()

HttpClient ( Client aClient)

Member Function Documentation

◆ available()

virtual int available ( )
virtual

◆ beginRequest()

void beginRequest ( )

Start a more complex request. Use this when you need to send additional headers in the request, but you will also need to call endRequest() when you are finished.

◆ completed()

virtual bool completed ( )
virtual

◆ connect() [1/2]

virtual int connect ( IPAddress  ip,
uint16_t  port 
)
virtual

◆ connect() [2/2]

virtual int connect ( const char *  host,
uint16_t  port 
)
virtual

◆ connected()

virtual uint8_t connected ( )
virtual

◆ contentLength()

int contentLength ( )

Return the length of the body.

Returns
Length of the body, in bytes, or kNoContentLengthHeader if no Content-Length header was returned by the server

◆ endOfBodyReached()

bool endOfBodyReached ( )

Test whether the end of the body has been reached. Only works if the Content-Length header was returned by the server

Returns
true if we are now at the end of the body, else false

◆ endOfHeadersReached()

bool endOfHeadersReached ( )

Test whether all of the response headers have been consumed.

Returns
true if we are now processing the response body, else false

◆ endOfStream()

virtual bool endOfStream ( )
virtual

◆ endRequest()

void endRequest ( )

End a more complex request. Use this when you need to have sent additional headers in the request, but you will also need to call beginRequest() at the start.

◆ finishHeaders()

void finishHeaders ( )
protected

◆ finishRequest()

void finishRequest ( )

Finish sending the HTTP request. This basically just sends the blank line to signify the end of the request

◆ flush()

virtual void flush ( )
virtual

◆ get() [1/4]

int get ( const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a GET request.

Parameters
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerPortPort to connect to on the server
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ get() [2/4]

int get ( const char *  aServerName,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a GET request.

Parameters
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ get() [3/4]

int get ( const IPAddress &  aServerAddress,
const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a GET request. This version connects doesn't perform a DNS lookup and just connects to the given IP address.

Parameters
aServerAddressIP address of the server to connect to
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerPortPort to connect to on the server
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ get() [4/4]

int get ( const IPAddress &  aServerAddress,
const char *  aServerName,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a GET request. This version connects doesn't perform a DNS lookup and just connects to the given IP address.

Parameters
aServerAddressIP address of the server to connect to
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ httpResponseTimeout()

virtual uint32_t httpResponseTimeout ( )
virtual

◆ operator bool()

virtual operator bool ( )
virtual

◆ peek()

virtual int peek ( )
virtual

◆ post() [1/4]

int post ( const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a POST request.

Parameters
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerPortPort to connect to on the server
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ post() [2/4]

int post ( const char *  aServerName,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a POST request.

Parameters
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ post() [3/4]

int post ( const IPAddress &  aServerAddress,
const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a POST request. This version connects doesn't perform a DNS lookup and just connects to the given IP address.

Parameters
aServerAddressIP address of the server to connect to
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerPortPort to connect to on the server
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ post() [4/4]

int post ( const IPAddress &  aServerAddress,
const char *  aServerName,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a POST request. This version connects doesn't perform a DNS lookup and just connects to the given IP address.

Parameters
aServerAddressIP address of the server to connect to
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ put() [1/4]

int put ( const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a PUT request.

Parameters
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerPortPort to connect to on the server
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ put() [2/4]

int put ( const char *  aServerName,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a PUT request.

Parameters
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ put() [3/4]

int put ( const IPAddress &  aServerAddress,
const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a PUT request. This version connects doesn't perform a DNS lookup and just connects to the given IP address.

Parameters
aServerAddressIP address of the server to connect to
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerPortPort to connect to on the server
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ put() [4/4]

int put ( const IPAddress &  aServerAddress,
const char *  aServerName,
const char *  aURLPath,
const char *  aUserAgent = NULL 
)

Connect to the server and start to send a PUT request. This version connects doesn't perform a DNS lookup and just connects to the given IP address.

Parameters
aServerAddressIP address of the server to connect to
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aURLPathUrl to request
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ read() [1/2]

virtual int read ( )
virtual

Read the next byte from the server.

Returns
Byte read or -1 if there are no bytes available.

◆ read() [2/2]

virtual int read ( uint8_t *  buf,
size_t  size 
)
virtual

◆ readHeader()

int readHeader ( )

Read the next character of the response headers. This functions in the same way as read() but to be used when reading through the headers. Check whether or not the end of the headers has been reached by calling endOfHeadersReached(), although after that point this will still return data as read() would, but slightly less efficiently

Returns
The next character of the response headers

◆ resetState()

void resetState ( )
protected

Reset internal state data back to the "just initialised" state

◆ responseStatusCode()

int responseStatusCode ( )

Get the HTTP status code contained in the response. For example, 200 for successful request, 404 for file not found, etc.

◆ sendBasicAuth()

void sendBasicAuth ( const char *  aUser,
const char *  aPassword 
)

Send a basic authentication header. This will encode the given username and password, and send them in suitable header line for doing Basic Authentication.

Parameters
aUserUsername for the authorization
aPasswordPassword for the user aUser

◆ sendHeader() [1/3]

void sendHeader ( const char *  aHeader)

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

Parameters
aHeaderHeader line to send, in its entirety (but without the trailing CRLF. E.g. "Authorization: Basic YQDDCAIGES"

◆ sendHeader() [2/3]

void sendHeader ( const char *  aHeaderName,
const char *  aHeaderValue 
)

Send an additional header line. This is an alternate form of sendHeader() which takes the header name and content as separate strings. The call will add the ": " to separate the header, so for example, to send a XXXXXX header call sendHeader("XXXXX", "Something")

Parameters
aHeaderNameType of header being sent
aHeaderValueValue for that header

◆ sendHeader() [3/3]

void sendHeader ( const char *  aHeaderName,
const int  aHeaderValue 
)

Send an additional header line. This is an alternate form of sendHeader() which takes the header name and content separately but where the value is provided as an integer. The call will add the ": " to separate the header, so for example, to send a XXXXXX header call sendHeader("XXXXX", 123)

Parameters
aHeaderNameType of header being sent
aHeaderValueValue for that header

◆ sendInitialHeaders()

int sendInitialHeaders ( const char *  aServerName,
IPAddress  aServerIP,
uint16_t  aPort,
const char *  aURLPath,
const char *  aHttpMethod,
const char *  aUserAgent 
)
protected

Send the first part of the request and the initial headers.

Parameters
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerIPIP address of the server (only used if we're going through a proxy and aServerName is NULL
aPortPort of the server being connected to.
aURLPathUrl to request
aHttpMethodType of HTTP request to make, e.g. "GET", "POST", etc.
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ setHttpResponseTimeout()

virtual void setHttpResponseTimeout ( uint32_t  timeout)
virtual

◆ skipResponseHeaders()

int skipResponseHeaders ( )

Skip any response headers to get to the body. Use this if you don't want to do any special processing of the headers returned in the response. You can also use it after you've found all of the headers you're interested in, and just want to get on with processing the body.

Returns
HTTP_SUCCESS if successful, else an error code

◆ startRequest() [1/2]

int startRequest ( const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aHttpMethod,
const char *  aUserAgent 
)

Connect to the server and start to send the request.

Parameters
aServerNameName of the server being connected to.
aServerPortPort to connect to on the server
aURLPathUrl to request
aHttpMethodType of HTTP request to make, e.g. "GET", "POST", etc.
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ startRequest() [2/2]

int startRequest ( const IPAddress &  aServerAddress,
const char *  aServerName,
uint16_t  aServerPort,
const char *  aURLPath,
const char *  aHttpMethod,
const char *  aUserAgent 
)

Connect to the server and start to send the request.

Parameters
aServerAddressIP address of the server to connect to.
aServerNameName of the server being connected to. If NULL, the "Host" header line won't be sent
aServerPortPort to connect to on the server
aURLPathUrl to request
aHttpMethodType of HTTP request to make, e.g. "GET", "POST", etc.
aUserAgentUser-Agent string to send. If NULL the default user-agent kUserAgent will be sent
Returns
0 if successful, else error

◆ stop()

virtual void stop ( )
virtual

◆ write() [1/2]

virtual size_t write ( uint8_t  aByte)
virtual

◆ write() [2/2]

virtual size_t write ( const uint8_t *  aBuffer,
size_t  aSize 
)
virtual

Member Data Documentation

◆ iBodyLengthConsumed

int iBodyLengthConsumed
protected

◆ iClient

Client* iClient
protected

◆ iContentLength

int iContentLength
protected

◆ iContentLengthPtr

const char* iContentLengthPtr
protected

◆ iHttpResponseTimeout

uint32_t iHttpResponseTimeout
protected

◆ iProxyAddress

IPAddress iProxyAddress
protected

◆ iProxyPort

uint16_t iProxyPort
protected

◆ iState

tHttpState iState
protected

◆ iStatusCode

int iStatusCode
protected

◆ kContentLengthPrefix

const char* kContentLengthPrefix
staticprotected

◆ kHttpPort

const uint16_t kHttpPort =80
static

◆ kHttpResponseTimeout

const int kHttpResponseTimeout = 30*1000
staticprotected

◆ kHttpWaitForDataDelay

const int kHttpWaitForDataDelay = 1000
staticprotected

◆ kNoContentLengthHeader

const int kNoContentLengthHeader =-1
static

◆ kUserAgent

const char* kUserAgent
static