|
| 1 | +# HTTPClient for Arduino |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +An Arduino HTTP Client that uses the Arduino Ethernet Library to make HTTP requests |
| 6 | +and handle responses. |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +### Creating a HTTP Client |
| 11 | + |
| 12 | +HTTP Client works with the Arduino Ethernet Library. Before you can create |
| 13 | +a HTTP client you must initialize your ethernet connection with something |
| 14 | +like: |
| 15 | + |
| 16 | +```arduino |
| 17 | +Ethernet.begin(mac, ip); |
| 18 | +``` |
| 19 | + |
| 20 | +For details on this see http://arduino.cc/en/Reference/ServerBegin |
| 21 | + |
| 22 | +To create a client (in this example for pachube) you can simply call one of |
| 23 | +the constructors: |
| 24 | + |
| 25 | +```arduino |
| 26 | +// The address of the server you want to connect to (pachube.com): |
| 27 | +byte server[] = { 173,203,98,29 }; |
| 28 | +HTTPClient client("api.pachube.com",server); |
| 29 | +``` |
| 30 | + |
| 31 | +which is equivalent to |
| 32 | + |
| 33 | +```arduino |
| 34 | +HTTPClient client("api.pachube.com",server,80); |
| 35 | +``` |
| 36 | + |
| 37 | +Now you are ready to go. |
| 38 | + |
| 39 | +### Posting request |
| 40 | + |
| 41 | +HTTP client supports three types of requests: |
| 42 | + |
| 43 | +* normal GET request to get some data from a URL |
| 44 | +* POST requests to transfer bigger amount of data to a server |
| 45 | +* PUT request as sepcified by REST APIs |
| 46 | +* currently there is no need for DELETE requests - so they do not exist yet. |
| 47 | + |
| 48 | +The result of a request is a stream (aka FILE*) by that you can read the data |
| 49 | +without the need to keep the whole answer in memory - which would fail for most |
| 50 | +HTML pages. |
| 51 | + |
| 52 | +FILE* streams are a bit more unusual the normal Arduino streams. They have been |
| 53 | +choosen since you can use all the nice fprintff and fscanf routines of avr-libc. |
| 54 | +After reading the response from the stream it has to be closed with the method `closeStream(stream)` |
| 55 | + |
| 56 | +DO NOT FORGET IT. Each stream has some data attached and if you forget to close the |
| 57 | +stream you get a memory leak, slowly filling up the precious memory of the Arduino. |
| 58 | + |
| 59 | +The result code of a HTTP request can be read with getLastReturnCode(). It returns a |
| 60 | +integer containing the return code. 200 indicates that everything was ok. |
| 61 | +For further details refer to http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
| 62 | + |
| 63 | +The HTTPClient has also a debug mode which can be witched on and off by using debug() |
| 64 | +with parameter 0 as no debug and anything else Ð e.g. -1 Ð as enabling debug output. |
| 65 | +By default the debug code is disabled. If debug is enabled the complete request and |
| 66 | +response is printed out on the serial connection. |
| 67 | +Very useful if your request do not work. |
| 68 | + |
| 69 | +All request take a number of parameters (depending on the request type): |
| 70 | + |
| 71 | +* the URI - a string (char*) containing the uri - which is normally everything |
| 72 | + following the hostname of a URL for http://arduino.cc/en/Reference/HomePage |
| 73 | + it would be Reference/HomePage |
| 74 | +* optional parameters as key value pairs. Parameters are appended to a URL like |
| 75 | + http://myhost/the/uri?parameter-name=parameter-value&other=parameter |
| 76 | + parameters are values of the struct http_client_parameter. It is easiest to |
| 77 | + do this like: |
| 78 | + |
| 79 | +```arduino |
| 80 | +http_client_parameter parameters[] = { |
| 81 | + { "key","afad32216dd2aa83c768ce51eef041d69a90a6737b2187dada3bb301e4c48841" } |
| 82 | + ,{ NULL,NULL } |
| 83 | +}; |
| 84 | +``` |
| 85 | + |
| 86 | +* for POST and PUT request a string with additional data can be passed as a string. The data |
| 87 | + has to be in memory. Future Versions may have future features. |
| 88 | + |
| 89 | +* for all requests additional headers can be specified. It works exactly the same was as uri |
| 90 | + parameters: |
| 91 | + |
| 92 | +```arduino |
| 93 | +http_client_parameter pachube_api_header[] = { |
| 94 | + { "X-PachubeApiKey","afad32216dd2aa83c768ce51eef041d69a90a6737b2187dada3bb301e4c48841" } |
| 95 | + ,{ NULL,NULL } |
| 96 | +}; |
| 97 | +``` |
| 98 | + |
| 99 | +Even though the HTTPClient supports HTTP 1.1 request no keep alive requests are supported currently. |
| 100 | + |
| 101 | +## Contributions |
| 102 | + |
| 103 | +Thanks to [colagrosso](http://github.com/colagrosso) for fixing the URL encoding |
| 104 | +Thanks to [hex705](http://github.com/hex705) for properly porting it to Arduino 1.0 |
| 105 | + |
| 106 | +## Copyright and license |
| 107 | + |
| 108 | +HTTPClient is free software: you can redistribute it and/or modify |
| 109 | +it under the terms of the GNU General Public License as published by |
| 110 | +the Free Software Foundation, either version 3 of the License, or |
| 111 | +(at your option) any later version. |
| 112 | + |
| 113 | +HTTPClient is distributed in the hope that it will be useful, |
| 114 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 115 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 116 | +GNU General Public License for more details. |
| 117 | +You should have received a copy of the GNU General Public License |
| 118 | +along with HTTPClient. If not, see http://www.gnu.org/licenses/. |
0 commit comments