Skip to content

High level HTTP Request Library that gives a javascript fetch like API.

License

Notifications You must be signed in to change notification settings

instanceofMA/arduino-fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fetch for Arduino

fetch is a high level HTTP Request Library that gives you a javascript fetch like API. It also supports making asynchronous/non-blocking requests, check out this example and others.

RequestOptions options;
options.method = "POST";
// options.fingerprint = "DC 78 3C 09 3A 78 E3 A0 BA A9 C5 4F 7A A0 87 6F 89 01 71 4C";
options.caCert = "";
options.headers["Content-Type"] = "application/json";
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";

/** In JSON, this would look like:
 * options = {
 *      method: "POST",
 *      fingerprint: "DC 78 3C 09 3A 78 E3 A0 BA A9 C5 4F 7A A0 87 6F 89 01 71 4C",
 *      headers: { contentType: "application/json" },
 *      body: "{\"email\": \"[email protected]\", \"password\": \"test:80\"}",
 * }
 */

Response response = fetch("https://api.grandeur.tech/auth/login/?apiKey=grandeurkywxmoy914080rxf9dh05n7e", options);

Here's the link to the release article on instructables.

Installation

You can install fetch in your Arduino Project by:

  1. Installing from the Arduino Library Manager directly.
  2. Download this repo as a ZIP and import it into your project.

Include

#include <Fetch.h>

Usage

Response response = fetch(const char* url, RequestOptions options);
FetchClient client = fetch(const char* url, RequestOptions options, OnResponseCallback callback);

Request Options

RequestOptions options;

/**
    {
        method: "GET" || "POST" || "HEAD" || "PUT" || "DELETE",
        headers: { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": Automatic, "Host: FromURL, "User-Agent": "arduino-fetch", "Cookie": "", "Accept": "* /*", "Connection": "close", "Transfer-Encoding": "chunked" },
        body: "",
        redirect: "follow" || "manual", "error",
        follow: Integer,

    }
*/

RequestOptions options;
options.method = "POST";
options.headers["Content-Type"] = "application/json";
options.headers["Connection"] = "keep-alive";
options.body = "email=EMAIL&password=PASSWORD";

Request Body

String body;
// application/x-www-form-urlencoded
body = "email=EMAIL&password=PASSWORD";
// application/json
body = "{\"email\":\"EMAIL\", \"password\"=\"PASSWORD\"}";

Request Headers

Headers headers;

headers["Content-Type"] = "application/json";
headers["Connection"] = "keep-alive";
headers["Cookie"] = "abc=def,ghi=jkl"; // Not yet supported.

Response

Response response;

String data = response.text();
JSONBody data = response.json(); // Not yet supported.
const Blob = response.blob(); // Not yet supported.
const FormData = response.formData(); // Not yet supported.
unsigned int[] array = response.arrayBuffer(); // Not yet supported.

bool ok = response.ok;
int status = response.status;
String statusText = response.statusText;
bool redirected = response.redirected; // Not yet supported.
String type = response.type; // Not yet supported.
String response.headers["content-type"];
Headers headers = response.headers.raw(); // Not yet supported
const char* contentTypeHeader = headers["content-type"]; // Not yet supported.