HTTP Client

This page explains how to do HTTP requests from your mbed.

Library

Import libraryHTTPClient

A HTTP Client for the mbed networking libraries

Architecture

The HTTPClient is composed of:

  • The actual client (HTTPClient)
  • Classes that act as a data repository, each of which deriving from the IHTTPDataIn/IHTTPDataOut interfaces (HTTPText for text content, HTTPMap for key/value pairs)

HTTP Client Data Containers

  • HTTPText

Import library

Public Member Functions

HTTPText (char *str)
Create an HTTPText instance for output.
HTTPText (char *str, size_t size)
Create an HTTPText instance for input.

Protected Member Functions

virtual void readReset ()
Reset stream to its beginning Called by the HTTPClient on each new request.
virtual int read (char *buf, size_t len, size_t *pReadLen)
Read a piece of data to be transmitted.
virtual int getDataType (char *type, size_t maxTypeLen)
Get MIME type.
virtual bool getIsChunked ()
Determine whether the HTTP client should chunk the data Used for Transfer-Encoding header.
virtual size_t getDataLen ()
If the data is not chunked, get its size Used for Content-Length header.
virtual void writeReset ()
Reset stream to its beginning Called by the HTTPClient on each new request.
virtual int write (const char *buf, size_t len)
Write a piece of data transmitted by the server.
virtual void setDataType (const char *type)
Set MIME type.
virtual void setIsChunked (bool chunked)
Determine whether the data is chunked Recovered from Transfer-Encoding header.
virtual void setDataLen (size_t len)
If the data is not chunked, set its size From Content-Length header.
virtual bool getHeader (char *header, size_t maxHeaderLen)
Get a specific header.

Friends

class HTTPClient
class HTTPClient
  • HTTPMap

Import library

Public Member Functions

HTTPMap ()
Instantiates HTTPMap It supports at most 32 key/values pairs.
void put (const char *key, const char *value)
Put Key/Value pair The references to the parameters must remain valid as long as the clear() function is not called.
void clear ()
Clear table.

Protected Member Functions

virtual void readReset ()
Reset stream to its beginning Called by the HTTPClient on each new request.
virtual int read (char *buf, size_t len, size_t *pReadLen)
Read a piece of data to be transmitted.
virtual int getDataType (char *type, size_t maxTypeLen)
Get MIME type.
virtual bool getIsChunked ()
Determine whether the HTTP client should chunk the data Used for Transfer-Encoding header.
virtual size_t getDataLen ()
If the data is not chunked, get its size Used for Content-Length header.
virtual bool getHeader (char *header, size_t maxHeaderLen)
Get a specific header.

Friends

class HTTPClient

Includes

#include "HTTPClient.h"

Reference

Import library

Public Member Functions

HTTPClient ()
Instantiate the HTTP client.
void basicAuth (const char *user, const char *password)
Provides a basic authentification feature (Base64 encoded username and password) Pass two NULL pointers to switch back to no authentication.
HTTPResult get (const char *url, IHTTPDataIn *pDataIn, int timeout=HTTP_CLIENT_DEFAULT_TIMEOUT)
Execute a GET request on the URL Blocks until completion.
HTTPResult get (const char *url, char *result, size_t maxResultLen, int timeout=HTTP_CLIENT_DEFAULT_TIMEOUT)
Execute a GET request on the URL Blocks until completion This is a helper to directly get a piece of text from a HTTP result.
HTTPResult post (const char *url, const IHTTPDataOut &dataOut, IHTTPDataIn *pDataIn, int timeout=HTTP_CLIENT_DEFAULT_TIMEOUT)
Execute a POST request on the URL Blocks until completion.
HTTPResult put (const char *url, const IHTTPDataOut &dataOut, IHTTPDataIn *pDataIn, int timeout=HTTP_CLIENT_DEFAULT_TIMEOUT)
Execute a PUT request on the URL Blocks until completion.
HTTPResult del (const char *url, IHTTPDataIn *pDataIn, int timeout=HTTP_CLIENT_DEFAULT_TIMEOUT)
Execute a DELETE request on the URL Blocks until completion.
int getHTTPResponseCode ()
Get last request's HTTP response code.

Example

Here is a program that tests both GET and POST data over a 3G connection.

Import programVodafoneUSBModemHTTPClientTest

Example demonstrating the use of the Vodafone USB Modem library with the HTTP Client

This is the same program over Ethernet with demos of PUT & DELETE requests as well.

Import programHTTPClient_HelloWorld

Simple example demonstrating how to use GET & POST requests with the HTTP Client


4 comments on HTTP Client:

12 Sep 2012

i got this error on you super tweet example "cannot open source input file "TCPSocketConnection.h": No such file or directory" in file "HTTPClientHTTPClient.h", Line: 27, Col: 32" can you tellme the resion

13 Jun 2014

The problem still exists... If I import the HTTPClient library I get the saem error.... When I use the [fixit] button I can add the Socket Lib but that I get a lot of new errors...

WHY???:

19 Sep 2014

Under following cirumstances, I get no response with POST. In this example, I'm sending data to a php-script to save the values into a database. (I'm using a C027 micro-controller)

main.cpp

 //POST data
int retHTTP;
char str[512];
HTTPMap map;
HTTPText inText(str, 512);//strlen(str));
map.put("ID", sID);
     
retHTTP = http.post("http://mypage/script/script.php", map, &inText, 1000  );

script.php

<?php
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate"); // <- no response if this header is added to the script
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
....
echo "DONE!";
?>

If this header is in the script,no response value will be given: header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");

Without it, it works fine. I've tried this POST with another micro-controller (which is not running via mbed or this library) and it had no problems with this header. Is there a reason why it behaves like that?

The php-script saves the values into a database correctly.

01 Mar 2018

TCPSocketConnection is not available... can you please tell me which lib is suitable for this file.I tried with EthernateInterface lib.

Please log in to post comments.