HTTPClient

Some simple demo how to use the HTTPClient.

Library

Import Library:

http://mbed.org/projects/cookbook/svn/EMAC/lwip/trunk

Import precompiled Library:

http://mbed.org/projects/cookbook/svn/EMAC/lwip/precomp

HTTP Client Demos

Simple fetching a textfile.

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

DigitalOut led(LED1);
HTTPClient http;
/**
 * Request a short text file from the mbed server
 */
int main(void) {
  char result[64];
  http.get("http://mbed.org/media/misc/robots.txt", result);
  // Display the page content:
  printf("Content is: %s\n", result);
  // Work is done!
  while(1) {
    led = !led;
    wait(0.2);
  }
}
Source Code of mbed-get.bin (LPC1768 LPC2368).

Same for a Google search for "HelloWorld":

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

DigitalOut led(LED1);
HTTPClient http;
/**
 * Request a google search for HelloWorld and display the first 2000 characters
 * of the page source on the serial terminal.
 */
int main(void) {
  char url[256];
  char result[2000];
  // Insert the search term into the URL
  sprintf(url, "http://www.google.co.uk/search?hl=en&q=%s&btnG=Search&meta=", "mbed");
  // Request 2000 characters from the result. Default is 64.
  http.get(url, result, 2000);
  // Display the result
  printf("%s\n", result);
  // Work is done!
  while(1) {
    led = !led;
    wait(0.2);
  }
}
Source Code of google-string.bin (LPC1768 LPC2368).

Writing into files

The same HelloWorld search redirected into a file on the local disk:

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

DigitalOut led(LED1);
HTTPClient http;
LocalFileSystem local("local");
/**
 * Request a google search for HelloWorld and display the first 2000 characters
 * of the page source on the serial terminal.
 */
int main(void) {
  char url[256];
  // Open a file to write.
  FILE *fd = fopen("/local/hello.htm", "w");
  // Insert the search term into the URL
  sprintf(url, "http://www.google.co.uk/search?hl=en&q=%s&btnG=Search&meta=", "HelloWorld");
  // Request a page and store it into a file.
  http.get(url, fd);
  // Close the file.
  fclose(fd);
  // The file is written on the local disk.
  // "/hello.htm" Have a look.

  // Work is done!
  while(1) {
    led = !led;
    wait(0.2);
  }
}
Source Code of google-file.bin (LPC1768 LPC2368).

Post nothing

The first demo with a post request:

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

DigitalOut led(LED1);
HTTPClient http;
/**
 * Request a short text file from the mbed server
 */
int main(void) {
  char result[64];
  http.post("http://mbed.org/media/misc/robots.txt", "", result);
  // Display the page content:
  printf("Content is: %s\n", result);
  // Work is done!
  while(1) {
    led = !led;
    wait(0.2);
  }
}
Source Code of mbed-post.bin (LPC1768 LPC1768).

API

HTTPClientA simple Class to fetch HTTP Pages.
Functions
HTTPClientCreates an HTTPClient object.
~HTTPClientDestroys the HTTPClient class.
headersAdd header additional Information to the next post or get requests.
authEnables basic authentication.
getA simple get-request just insert the url.
getA simple get-request just insert the url and a FILE Pointer.
postA simple post-request just insert the url.
postA simple get-request just insert the url and a FILE Pointer.
postA simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results.
postA simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results.
timeoutSets the timout for a HTTP request.
class HTTPClient : public TCPConnection
A simple Class to fetch HTTP Pages.
HTTPClient(const char *hostname =  NULL,
struct ip_addr ip =  ip_addr_any,
struct ip_addr nm =  ip_addr_any,
struct ip_addr gw =  ip_addr_any,
struct ip_addr dns =  ip_addr_any)
Creates an HTTPClient object.
virtual ~HTTPClient()
Destroys the HTTPClient class.
void headers(const char *fields)
Add header additional Information to the next post or get requests.
void auth(const char *user,
const char *password)
Enables basic authentication.
unsigned int get(const char *url,  
char *result =  NULL,
int rsize =  64)
A simple get-request just insert the url.
unsigned int post(const char *url,  
const char *data =  NULL,
char *result =  NULL,
int rsize =  64)
A simple post-request just insert the url.
void timeout(int value)
Sets the timout for a HTTP request.

Resources

  • HTTPClient/trunk - Have a look at the API for more demos
  • Twitter - or at the twitter example for a more complex example.
  • Timetric - or at the timetric example for a more complex example.

Development Log