https://github.com/csquared/arduino-restclient
Arduino RESTful HTTP Request Library
https://github.com/csquared/arduino-restclient
Last synced: 5 months ago
JSON representation
Arduino RESTful HTTP Request Library
- Host: GitHub
- URL: https://github.com/csquared/arduino-restclient
- Owner: csquared
- License: mit
- Created: 2013-02-28T21:12:53.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2018-12-04T22:53:50.000Z (over 6 years ago)
- Last Synced: 2024-04-14T08:46:00.038Z (about 1 year ago)
- Language: C++
- Homepage:
- Size: 60.5 KB
- Stars: 205
- Watchers: 16
- Forks: 133
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- Awesome-arduino - arduino-restclient - Arduino RESTful HTTP Request Library (Libraries)
- awesome-arduino - arduino-restclient - Arduino RESTful HTTP Request Library (Libraries)
README
# 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.
```c++
#include
#include
#include "RestClient.h"
```### RestClient(host/ip, [port])
Constructor to create an RestClient object to make requests against.
Use domain name and default to port 80:
```c++
RestClient client = RestClient("arduino-http-lib-test.herokuapp.com");
```Use a local IP and an explicit port:
```c++
RestClient client = RestClient("192.168.1.50",5000);
```### dhcp()
Sets up `EthernetClient` with a mac address of `DEADBEEFFEED`. Returns `true` or `false` to indicate if setting up DHCP
was successful or not```c++
client.dhcp()
```Note: you can have multiple RestClient objects but only need to call
this once.Note: if you have multiple Arduinos on the same network, you'll need
to give each one a different mac address.### begin(byte mac[])
It just wraps the `EthernetClient` call to `begin` and DHCPs.
Use this if you need to explicitly set the mac address.```c++
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
if (client.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
```### Manual Ethernet Setup
You can skip the above methods and just configure the EthernetClient yourself:
```c++
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 192, 168, 2, 11 };
Ethernet.begin(mac,ip);
``````c++
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Ethernet.begin(mac);
```This is especially useful for debugging network connection issues.
## 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!
```c++
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:
```c++
#define HTTP_DEBUG
```Everything happening in the client will get printed to the Serial port.
# Thanks
[ricardochimal](https://github.com/ricardochimal) For all his c++ help. Couldn't have done this without you!
[theycallmeswift](https://github.com/theycallmeswift) Helping incept and debug v1.0