HTTP Client Data Containers

Data Containers

There are four containers included in the HTTPClient package:

  • HTTPText
  • HTTPMap
  • HTTPFile
  • HTTPStream

You can also create your own by simply inheriting the HTTPData class.

HTTPText

This is a simple "Text" data repository.

Reference

Import program

Public Member Functions

HTTPText (const string &encoding="text/html", int maxSize=DEFAULT_MAX_MEM_ALLOC)
Instantiates the object.
const char * gets () const
Gets text.
void puts (const char *str)
Sets the text in the container using a zero-terminated char*.
string & get ()
Gets text.
void set (const string &str)
Puts text.
virtual void clear ()
Clears the content.

Friends

class HTTPClient

Examples

The Working with the networking stack article's first example uses HTTPText.

HTTPMap

This class simplifies the use of key/value pairs requests and responses used widely among web APIs.

Inheritance

Note that HTTPMap inherits from std::map<std::string,std::string>. You can therefore use any public method of that class, including the square brackets operator ( [ ] ) to access a value.

Reference

Import program

Public Member Functions

HTTPMap (const string &keyValueSep="=", const string &pairSep="&")
Instantiates map.
virtual void clear ()
Clears the content.

Friends

class HTTPClient

Examples

The Twitter example uses HTTPMap.

HTTPFile

This is class provides file access/storage for HTTP requests and responses data payloads.

Reference

Import program

Public Member Functions

HTTPFile (const char *path)
Instantiates data source/sink with file in param.
virtual void clear ()
Forces file closure.

Friends

class HTTPClient

Examples

HTTPStream

This class allows you to stream data from the web using a persisting HTTP connection. To use it properly you must use a non-blocking HTTPClient method.

Reference

Import program

Public Member Functions

HTTPStream ()
Instantiates the object.
void readNext (byte *buf, int size)
Starts to read into buffer.
bool readable ()
Returns whether there is data available to read.
int readLen ()
Returns the actual length of the payload written in the buffer.

Friends

class HTTPClient

Examples

This example displays the Hackaday RSS feed while loading.

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"

EthernetNetIf eth; 
HTTPClient http;

HTTPResult result;
bool completed = false;
void request_callback(HTTPResult r)
{
  result = r;
  completed = true;
}

int main() {

  printf("Start\n");

  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup OK\n");
  
  HTTPStream stream;
  
  char BigBuf[512 + 1] = {0};
  stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
  
  HTTPResult r = http.get("HTTP://hackaday.com/feed/", &stream, request_callback); //Load a very large page, such as the hackaday RSS feed
  
  while(!completed)
  {
    Net::poll(); //Polls the Networking stack
    if(stream.readable())
    {
      BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
      printf("%s",BigBuf); //Display it while loading
      //Note: some servers do not like if you throttle them too much, so printf'ing during a request is generally bad practice
      stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
    }
  }
  printf("\n--------------\n");
  if(result == HTTP_OK)
  {
    printf("Read completely\n"); 
  }
  else
  {
    printf("Error %d\n", result);
  }
  
  while(1)
  {
  
  }
  
  return 0;
  
}

You can import this program from here: http://mbed.org/users/donatien/programs/HTTPClientStreamingExample


All wikipages