Releases: instanceofMA/arduino-fetch
Fetch now supports making asynchronous requests.
You can now pass a callback function to fetch
and fetch
will pass the response to that callback function when it's received instead of blocking the rest of the code until it receives the response. Here's how you can do it:
// Fetch, in async mode, returns a FetchClient instead of returning the response.
FetchClient client;
void handleResponse(Response response) {
Serial.println(response);
}
void setup() {
// Setting options.
RequestOptions options;
options.method = "GET";
// Making the request.
client = fetch("http://api.grandeur.tech", options, handleResponse);
Serial.println("This now prints before the response is printed.");
}
void loop() {
// This is crucial, it listens on the connection for the response, until it's received.
client.loop()
}
If you want to do more than one fetch
, you need to create more than one FetchClient
variables and more than one client.loop()
s.
Only if you are doing fetch
sequentially, that is, the next fetch
happens after the previous fetch
response has returned, then you can reuse the same FetchClient
.
Support for Custom Headers
Library syntax has updated and dot notation (headers.contentType) for setting headers in RequestOptions
is dropped in favor of square-brackets notation headers["Content-Type"]
because the latter supports custom headers:
This will break:
RequestOptions options;
options.headers.contentType = "application/json";
This now works:
RequestOptions options;
options.headers["Content-Type"] = "application/json";
Syntax for accessing response headers is also updated:
Old syntax:
response.headers.get("Content-Type");
New syntax:
response.headers["Content-Type"];
Thank you all for your feedback. Keep supporting! ❤️
Full Changelog: 0.0.3...0.1.0
Fetch for Arduino: v0.0.3
Provides support for ESP32 with CACert option:
#define CACert "-----BEGIN CERTIFICATE-----\n\
MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n\
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n\
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n\
ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\n\
MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\n\
LmRpZ2ljZXJ0Lm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://api.github.com", options);
Already works with ESP8266 with fingerprint option:
#define FINGERPRINT "96 84 07 DF 0B 1C F6 58 14 DF D7 33 35 57 51 9B 15 4D 8C E7"
options.fingerprint = FINGERPRINT;
Response response = fetch("https://api.github.com/", options);
First release of Fetch for Arduino.
This release implements the basic functionalities that can be found in the Javascript fetch API.
You can make GET and POST request to any HTTP and HTTPS server. In case of HTTPS server, you need to provide the server's SHA1 fingerprint which you can find by opening the webpage in browser, clicking on the greenlock, and going into the certificate details.
In Javascript, fetch API works like:
const response = await fetch("https://api.grandeur.tech/", {
method: "GET"
});
console.log(response);
In Arduino, you can do this:
RequestOptions options;
options.method = "GET";
Response response = fetch("https://api.grandeur.tech/", options);
Serial.println(response);
You can also provide your headers to the request like this:
options.headers.contentType = "application/json";
You can also provide your body to the request like this:
options.body = "{\"email\": \"EMAIL\"}";
The response
object gives you the following data:
response.status = 200;
response.statusText = "OK";
You can also get response headers like this:
response.headers.get("Content-Type");