You are viewing an older revision! See the latest version

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

HttpText(const string& encoding = "text/html", int maxSize = DEFAULT_MAX_MEM_ALLOC)

Instantiates the object. You can set the encoding of the data here, it defaults to text/html. The maxSize attribute defines the maximum memory size that can be allocated by the object. It defaults to 512 bytes.

const char* gets() const

Returns the text in the container as a zero-terminated char*. The array returned points to the internal buffer of the object and remains owned by the object.

void puts(const char* str)

Sets the text in the container using a zero-terminated char*.

string& get()

Returns the text in the container as string.

void set(const string& str)

Sets the text in the container as string.

void clear()

Clears the content. If this container is used as a data sink, it is cleared by the HTTP Client at the beginning of the request.

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

HttpMap()

Instantiates the object.

void clear()

Clears the content. If this container is used as a data sink, it is cleared by the HTTP Client at the beginning of the request.

Examples

The Twitter example uses HttpMap.

HttpFile

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

Reference

HttpFile(const char* path)

Instantiates the object. Opens file at path path. It will be opened when some data has to be read/written from/to it and closed when this operation is complete or on destruction of the instance.

Note that you must choose the file will be opened with mode "w" for writing and mode "r" for reading, so the file will be cleared between each request if you are using it for writing.

Information

Note that to use this you must instantiate a proper file system (such as the LocalFileSystem or the SDFileSystem).

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

HttpStream()

Instantiates the object.

void readNext(byte* buf, int size)

Passes a buffer of address buf and size size to the instance. When it receives data it will be stored in this buffer. When the buffer is full it throttles the client until this function is called again.

bool readable()

Returns true if there is data available to read.

int readLen()

Returns the actual length of the payload written in the buffer.

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("\r\nSetting up...\r\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("\r\nSetup OK\r\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 is zero-terminated char* string
      printf("%s",BigBuf); //Displays it while loading
      //Note: some server 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("\r\n--------------\r\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/NetHttpClientStreamingExample


All wikipages