Git Product home page Git Product logo

httpclient's Introduction

HTTPClient for Arduino

Overview

An Arduino HTTP Client that uses the Arduino Ethernet Library to make HTTP requests and handle responses.

Usage

Creating a HTTP Client

HTTP Client works with the Arduino Ethernet Library. Before you can create a HTTP client you must initialize your ethernet connection with something like:

Ethernet.begin(mac, ip);

For details on this see http://arduino.cc/en/Reference/ServerBegin

To create a client (in this example for pachube) you can simply call one of the constructors:

//  The address of the server you want to connect to (pachube.com):
byte server[] = { 173,203,98,29 }; 
HTTPClient client("api.pachube.com",server);

which is equivalent to

HTTPClient client("api.pachube.com",server,80);

Now you are ready to go.

Making a request

HTTP client supports three types of requests:

  1. GET requests to get some data from a URL
  2. POST requests to transfer a larger amount of data to a server
  3. PUT requests as sepcified by REST APIs

DELETE requests have not yet been implemented.

All request take a number of parameters (depending on the request type):

http_client_parameter parameters[] = {
  { "key","afad32216dd2aa83c768ce51eef041d69a90a6737b2187dada3bb301e4c48841" }
  ,{ NULL,NULL }
};
  • For POST and PUT request a string with additional data can be passed as a string. The data has to be in memory. Future Versions may have future features.
  • For all requests additional headers can be specified. It works exactly the same was as uri parameters:
http_client_parameter pachube_api_header[] = {
  { "X-PachubeApiKey","afad32216dd2aa83c768ce51eef041d69a90a6737b2187dada3bb301e4c48841" }
  ,{ NULL,NULL }
};

Even though the HTTPClient supports HTTP 1.1 request no keep alive requests are supported currently.

Handling a response

The result code of a HTTP request can be read with getLastReturnCode(). It returns a integer containing the return code. 200 indicates that everything was ok. For further details refer to http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

The result of a request is a stream (aka FILE*) by that you can read the data without the need to keep the whole answer in memory - which would fail for most HTML pages.

FILE* streams are a bit more unusual the normal Arduino streams. They have been choosen since you can use all the nice fprintff and fscanf routines of avr-libc.

After reading the response from the stream it has to be closed with the method:

closeStream(stream)

DO NOT FORGET IT. Each stream has some data attached and if you forget to close the stream you get a memory leak, slowly filling up the precious memory of the Arduino.

Debug mode

The HTTPClient has also a debug mode which can be switched on and off by using debug() with parameter 0 as no debug and anything else ร e.g. -1 ร as enabling debug output. By default the debug code is disabled. If debug is enabled the complete request and response is printed out on the serial connection. Very useful if your request does not work.

Contributions

  • Thanks to colagrosso for fixing the URL encoding
  • Thanks to hex705 for properly porting it to Arduino 1.0

Copyright and license

HTTPClient is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

HTTPClient is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with HTTPClient. If not, see http://www.gnu.org/licenses/.

httpclient's People

Contributors

interactive-matter avatar hex705 avatar alexanderdean avatar apollon77 avatar colagrosso avatar ppicazo avatar

Watchers

James Cloos avatar Vinicius Biscolla avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.