HTTP Client

This page explains how to do HTTP requests from your mbed. It uses the Networking stack.

Hello World!

If you haven't done it yet, try out the simple Working with the networking stack example program.

Packages

Precompiled version:

Library

Architecture

The HTTPClient is composed of:

  • The actual client (HTTPClient)
  • Classes that act as a data repository, each of which deriving from the HTTPData class (HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)

HTTP Client Data Containers

  • HTTPText
  • HTTPMap
  • HTTPFile
  • HTTPStream

See HTTP Client Data Containers

Includes

#include "HTTPClient.h"

Reference

» Import this program

Public Member Functions

  HTTPClient ()
  Instantiates the HTTP client.
void  basicAuth (const char *user, const char *password)
  Provides a basic authentification feature (Base64 encoded username and password)
HTTPResult   get (const char *uri, HTTPData *pDataIn)
  Executes a GET Request (blocking)
HTTPResult   get (const char *uri, HTTPData *pDataIn, void(*pMethod)( HTTPResult ))
  Executes a GET Request (non blocking)
template<class T >
HTTPResult   get (const char *uri, HTTPData *pDataIn, T *pItem, void(T::*pMethod)( HTTPResult ))
  Executes a GET Request (non blocking)
HTTPResult   post (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn)
  Executes a POST Request (blocking)
HTTPResult   post (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn, void(*pMethod)( HTTPResult ))
  Executes a POST Request (non blocking)
template<class T >
HTTPResult   post (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn, T *pItem, void(T::*pMethod)( HTTPResult ))
  Executes a POST Request (non blocking)
void  doGet (const char *uri, HTTPData *pDataIn)
  Executes a GET Request (non blocking)
void  doPost (const char *uri, const HTTPData &dataOut, HTTPData *pDataIn)
  Executes a POST Request (non blocking)
void  setOnResult (void(*pMethod)( HTTPResult ))
  Setups the result callback.
void  setTimeout (int ms)
  Setups timeout.
virtual void  poll ()
  This method can be inherited so that it is called on each Net::poll() call.
int  getHTTPResponseCode ()
  Gets last request's HTTP response code.
void  setRequestHeader (const string &header, const string &value)
  Sets a specific request header.
string &  getResponseHeader (const string &header)
  Gets a response header.
void  resetRequestHeaders ()
  Clears request headers.

Protected Member Functions

void  close ()
  This flags the service as to be destructed if owned by the pool.

Examples

Have a look at the Working with the networking stack and Twitter examples.

License





10 comments:

28 Oct 2010

I'm using the HTTPclient to stream from Twitter, It works for a while, then data just stops flowing. If I use curl on the desktop, the stream never stops. I don't get any errors back from the HTTP library. Any ideas?

18 Nov 2010

I want to call a non-blocking get() from inside of a class. But the API isn't designed for that, since the calls take only a static (or global) method, which would not allow me to instantiate multiple objects of my class. Can we have additional methods which take an additional void* parameter, and pass it as first argument to the callback function? (see http://www.parashift.com/c++-faq-lite/pointers-to-members.html for more on that).

Thanks,

hli

18 Jun 2011

How do i post data to other web page beside twitter?

07 Sep 2011

user Pier Juhng Hiah wrote:

How do i post data to other web page beside twitter?

I'm thinking that too!

07 Sep 2011

user Merlin Spiers wrote:

user Pier Juhng Hiah wrote:

How do i post data to other web page beside twitter?

I'm thinking that too!

here it is:

oid doPost	(	const char * 	uri,
const HTTPData & 	dataOut,
HTTPData * 	pDataIn	 
)			
Executes a POST Request (non blocking).

Executes a POST request on the URI uri

Parameters:
uri 	: URI on which to execute the request
dataOut 	: a HTTPData instance that contains the data that will be posted
pDataIn 	: pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
pMethod 	: callback function The function returns immediately and calls the previously set callback on completion or error
Definition at line 123 of file HTTPClient.cpp.

05 Mar 2012

dear all,

How do I encode the next statement properly !

char url[192] = {0};
sprintf(url, "http://192.168.1.80/use_mon/sms_send.php?unit=%d&mess=Water_Alert: %.1f Ltr", 1, 203.4);
HTTPText txt;
HTTPResult r = http.get(url, &txt);

as it will not send out any spaces like the ones after the : and before L properly ???

as the next statement seems to work

sprintf(url, "http://192.168.1.80/use_mon/sms_send.php?unit=%d&mess=Water_Alert:%.1fLtr", 1, 203.4);

Thanks for any help in encoding it properly

Paul

06 Mar 2012

Using a + character instead of a space solves the problem in the uri part of the http.get call.

16 May 2012

Hi,

Been playing with the HTTP client and all works well in the mbed compiler. However, I tried the compiler export function to try and use the code in an offline Keil Vision4 project. Getting errors at the linker along the lines of :-

.\build\HTTPClientExampleV2.axf: Error: L6218E: Undefined symbol typeinfo for NetIf (referred from lwipnetif.o).

Has anyone yet tried and succeeded in getting the HTTP client (or just NetServices) to work using the Keil Vision IDE ?

21 May 2012

Stumped on HTTP client problem... Using an HTTPS: URL, connecting to google's calendar access via private password. Works fine if google calendar has just one event to return from the URL time-span I pass. This is about 2700 bytes. I'm using a blocking http.get(). If there are 2 calendar events in the time window, the HTTP client get function hangs. never returns, never times out. My declaration of the data buffer is increased to huge... HTTPText responseText("text/html", 8192);

any ideas?

ALSO... I see an http client dated 2012. And the original code a year or more old. How do I know which version is in the netservices library?

25 Sep 2012

Hi, can somebody tell me how to set the http header information with this client?