HttpPostClient

class mono::network::HttpPostClient

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.)

Inherits from mono::network::HttpClient

Public Functions

HttpPostClient()

Construct an empty invalid HTTP client.

HttpPostClient(String anUrl, String headers = String ())

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.

Parameters
  • context: pointer to the object to call the member function on
  • method: pointer to the member function to be called

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.

Parameters
  • context: pointer to the object to call the member function on
  • method: pointer to the member function to be called

void post()

Execute the POST request.

Commit the request sending it to the server.

Protected Functions

void dnsComplete(INetworkRequest::CompletionEvent *evnt)

dns resolver completion event handler