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/NetServicesLPC1768
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
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.