HttpPostClient

class

HTTP POST Request Client.

This class can send a HTTP POST request with body data. You provide callbacks to provide the body length (in bytes) and content. This is to avoid caching the content, and save memory.

Before this class works you must setup a working wifi connection.

Example

HttpPostClient client("http://www.postrequest.org/");
client.setBodyLengthCallback<MyClass>(this, &MyClass::getDataLength);
client.setBodyDataCallback<MyClass>(this, &MyClass::getData);
client.post();

This will post the request with no default Content-Type header. You can provide your own Content-Type by using the second (optional) argument in the constructor. If you do, remember to insert a \r\n at the end of of the header string.

Trailing white space

Due to a bug in the Redpine module protocol, the post body risk getting trailing whitespace characters (spaces: 0x20). There can up to 3 trailing spaces: . The reason is the redpine frame must end on a 4-byte byte boundry. Instead of appending NULL characters, we append spaces. Most HTTP servers does not respond well to NULL character being part of a text body.

This is of course an issue when sending binary data via POST requests. In these cases you should (at this point) avoid binary formats. (Maybe use base64 encoding.)

Public Functions

HttpPostClient::HttpPostClient()

Construct an empty invalid HTTP client.

HttpPostClient::HttpPostClient(String anUrl, String headers)

Construct a client with a target URL.

Headers like Host and Content-Length are created automatically. The DNS resolution of the hostname is also done automatically.

The actual POST request is not execute before you call post. However, DNS resolution is started immediately.

Parameters
  • anUrl -

    THe URL to call

  • headers -

    Optional extra headers to add to the request

template <typename Class>
void mono::network::HttpPostClient::setBodyLengthCallback(Class * context, uint16_t(Class::*)(void) method)

Callback for providing the length of the HTTP POST body.

You should provide a efficient method of getting the body length, since this callback is used multiple times under the request execution.

NOTE: The length returned must be the real HTTP Content-length. This means any C string terminator should not be included.

Parameters
  • context -

    pointer to the object to call the member function on

  • method -

    pointer to the member function to be called

void mono::network::HttpPostClient::setBodyLengthCallback(uint16_t (*cfunc)(void))

Callback for providing the length of the HTTP POST body.

You should provide a efficient method of getting the body length, since this callback is used multiple times under the request execution.

NOTE: The length returned must be the real HTTP Content-length. This means any C string terminator should not be included.

Parameters
  • cfunc -

    Pointer to the function to call for request data length

template <typename Class>
void mono::network::HttpPostClient::setBodyDataCallback(Class * context, void(Class::*)(char *) method)

Callback for providing the body content of the HTTP POST.

The internals of the request will ensure the provided char* is large enough to hold the size of the HTTP body, including the the string terminator character.

*The buffer pointer provided, points to a buffer that is the length you provided from setBodyLengthCallback plus a terminator character.*

Parameters
  • context -

    pointer to the object to call the member function on

  • method -

    pointer to the member function to be called

void mono::network::HttpPostClient::setBodyDataCallback(void (*cfunc)(char *))

Callback for providing the body content of the HTTP POST.

The internals of the request will ensure the provided char* is large enough to hold the size of the HTTP body, including the the string terminator character.

*The buffer pointer provided, points to a buffer that is the length you provided from setBodyLengthCallback plus a terminator character.*

Parameters
  • cfunc -

    Pointer to the function to call for request body data

void HttpPostClient::post()

Execute the POST request.

Commit the request sending it to the server.

Protected Functions

virtual void HttpPostClient::dnsComplete(INetworkRequest::CompletionEvent *evnt)

dns resolver completion event handler