Skip to content

paolobueno/arduino-restclient

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RestClient for Arduino

HTTP Request library for Arduino and the Ethernet shield.

Install

Clone (or download and unzip) the repository to ~/Documents/Arduino/libraries where ~/Documents/Arduino is your sketchbook directory.

> cd ~/Documents/Arduino
> mkdir libraries
> cd libraries
> git clone https://github.com/csquared/arduino-restclient.git RestClient

Usage

Include

You need to have the Ethernet library already included.

#include <Ethernet.h>
#include <SPI.h>
#include "RestClient.h"

RestClient(host/ip, [port], Client)

Constructor to create an RestClient object to make requests against.

Use domain name and default to port 80:

EthernetClient client
RestClient client = RestClient("arduino-http-lib-test.herokuapp.com", client);

Use a local IP and an explicit port:

EthernetClient client
RestClient client = RestClient("192.168.1.50", 5000, client);

Ethernet Setup

You should configure an EthernetClient-compatible client and supply it to the library:

EthernetClient client
RestClient restClient("arduino-http-lib-test.herokuapp.com", client);
void setup() {
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  //the IP address for the shield:
  byte ip[] = { 192, 168, 2, 11 };
	Ethernet.begin(mac, ip);
}

See the official documentation for Ethernet for more information.

RESTful methods

All methods return an HTTP status code or 0 if there was an error.

get(const char* path)

get(const char* path, String* response)

Start making requests!

int statusCode = client.get("/"));

Pass in a string by reference for the response:

String response = "";
int statusCode = client.get("/", &response);

post(const char* path, const char* body)

post(const char* path, String* response)

post(const char* path, const char* body, String* response)

String response = "";
int statusCode = client.post("/", &response);
statusCode = client.post("/", "foo=bar");
response = "";
statusCode = client.post("/", "foo=bar", &response);

put(const char* path, const char* body)

put(const char* path, String* response)

put(const char* path, const char* body, String* response)

String response = "";
int statusCode = client.put("/", &response);
statusCode = client.put("/", "foo=bar");
response = "";
statusCode = client.put("/", "foo=bar", &response);

del(const char* path)

del(const char* path, const char* body)

del(const char* path, String* response)

del(const char* path, const char* body, String* response)

String response = "";
int statusCode = client.del("/", &response);

Full Example

I test every way of calling the library (against a public heroku app)[https://github.com/csquared/arduino-http-test].

You can find the file in File->Examples->RestClient->full_test_suite

Debug Mode

If you're having trouble, you can always open RestClient.cpp and throw at the top:

#define HTTP_DEBUG

Everything happening in the client will get printed to the Serial port.

Thanks

ricardochimal For all his c++ help. Couldn't have done this without you!

theycallmeswift Helping incept and debug v1.0

About

Arduino RESTful HTTP Request Library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 100.0%