You are viewing an older revision! See the latest version

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: http://mbed.org/users/donatien/programs/HTTPClient

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

HTTPClient()

Instantiate the HTTP client.

Requests

HTTPResult get(const char* uri, HTTPData* pDataIn) //Blocking
HTTPResult get(const char* uri, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
HTTPResult get(const char* uri, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking

Executes a GET request on the URI uri. pDataIn is a pointer to an HTTPData instance that will collect the data returned by the request. pDataIn can be NULL.

If a callback is provided, the function returns immediately and calls the callback on completion or error, otherwise it blocks until completion.

HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn) //Blocking
HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, void (*pMethod)(HTTPResult)) //Non blocking
HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking  

Executes a POST request on the URI uri. dataOut is a HTTPData instance that contains the data that will be posted. pDataIn is a pointer to an HTTPData instance that will collect the data returned by the request. pDataIn can be NULL.

If a callback is provided, the function returns immediately and calls the callback on completion or error, otherwise it blocks until completion.

void setOnResult( void (*pMethod)(HTTPResult) )
void setOnResult( T* pItem, void (T::*pMethod)(HTTPResult) )
void doGet(const char* uri, HTTPData* pDataIn)
void doPost(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn)

Alternatively, you can set a callback function for all requests using setOnResult, and then call the non-blocking doGet and doPost methods.

Parameters

void setTimeout(int ms)

Sets the time in milliseconds before a request should be considered as having timed out.

int getHTTPResponseCode()

After completion of the request, get the HTTP response code sent back by the server.

void basicAuth(const char* user, const char* password)

Provides a basic authentification feature (Base64 encoded username and password).

void setRequestHeader(const string& header, const string& value)

Set a specific request header.

string& getResponseHeader(const string& header)

Get specific response header.

void resetRequestHeaders()

Request headers are not cleared after each request, to force this use that function.

Result codes

enum HTTPResult
{
  HTTP_OK,
  HTTP_PROCESSING,
  HTTP_PARSE, //URI Parse error
  HTTP_DNS, //Could not resolve name
  HTTP_PRTCL, //Protocol error
  HTTP_NOTFOUND, //404 Error
  HTTP_REFUSED, //403 Error
  HTTP_ERROR, //xxx error
  HTTP_TIMEOUT, //Connection timeout
  HTTP_CONN //Connection error
};

Examples

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

License


All wikipages