Skip to content

Commit

Permalink
ESP32 is tested! Readme.md is corrected.
Browse files Browse the repository at this point in the history
  • Loading branch information
instanceofMA committed Feb 2, 2022
1 parent d736b83 commit f58b645
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 30 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ fetch is a high level HTTP Request Library that gives you a javascript fetch lik
```js
ResponseOptions 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.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.contentType = "application/json";
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";

Expand Down Expand Up @@ -102,10 +103,10 @@ unsigned int[] array = response.arrayBuffer(); // Not yet supported.

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

Expand Down
25 changes: 24 additions & 1 deletion examples/esp32/get/get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@

#define SSID YourWiFiSSID
#define PASSPHRASE YourWiFiPassphrase

#define CACert "-----BEGIN CERTIFICATE-----\n\
MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n\
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n\
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n\
ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDA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S+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n\
+OkuE6N36B9K\n\
-----END CERTIFICATE-----"

void setup() {
Serial.begin(9600);
connectWiFi(SSID, PASSPHRASE);

RequestOptions options;
options.method = "GET";
options.caCert = CACert;

Response response = fetch("https://api.github.com/", options);

Expand Down
1 change: 1 addition & 0 deletions examples/esp32/get/recipes/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void connectWiFi(const char* ssid, const char* passphrase) {
while (WiFi.status() != WL_CONNECTED) {
// Print dots in a horizontal line to the Serial, showing the WiFi is trying to connect.
Serial.print(".");
delay(1000);
// Blink LED very fast, showing the WiFi is trying to connect.
// blinkN(10);
}
Expand Down
33 changes: 32 additions & 1 deletion examples/esp32/post/post.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@

#define SSID YourWiFiSSID
#define PASSPHRASE YourWiFiPassphrase
#define CACert "-----BEGIN CERTIFICATE-----\n\
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n\
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n\
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n\
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n\
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n\
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n\
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+\n\
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60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qpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n\
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n\
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n\
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n\
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n\
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n\
-----END CERTIFICATE-----"

void setup() {
Serial.begin(9600);
Expand All @@ -12,10 +43,10 @@ void setup() {
options.method = "POST";
options.headers.contentType = "application/json";
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";
options.caCert = CACert;

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

Serial.println(response)
// Printing response.
Serial.println(response);
// Printing respons headers.
Expand Down
1 change: 1 addition & 0 deletions examples/esp32/post/recipes/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void connectWiFi(const char* ssid, const char* passphrase) {
// Print dots in a horizontal line to the Serial, showing the WiFi is trying to connect.
Serial.print(".");
// Blink LED very fast, showing the WiFi is trying to connect.
delay(1000);
// blinkN(10);
}
// Stop the LED blinking, showing the WiFi is successfully connected.
Expand Down
4 changes: 2 additions & 2 deletions examples/esp8266/get/get.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "recipes/WiFi.h"
#include "Fetch.h"

#define SSID "MA"
#define PASSPHRASE "nopassword"
#define SSID YourWiFiSSID
#define PASSPHRASE YourWiFiPassphrase
#define FINGERPRINT "96 84 07 DF 0B 1C F6 58 14 DF D7 33 35 57 51 9B 15 4D 8C E7"


Expand Down
22 changes: 9 additions & 13 deletions src/Fetch.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "Fetch.h"
#include <WiFiClientSecure.h>

// Fetch fetch;

Response fetch(const char* url, RequestOptions options) {
// Parsing URL.
Url parsedUrl = parseUrl(url);
Expand All @@ -14,10 +12,11 @@ Response fetch(const char* url, RequestOptions options) {
// Set fingerprint if https.
if(parsedUrl.scheme == "https") {
#ifdef ESP8266
if(options.fingerprint == "") {
Serial.println("[INFO] No fingerprint is provided. Using the INSECURE mode for connection!");
if(options.fingerprint == "" && options.caCert == "") {
Serial.println("[INFO] No fingerprint or caCert is provided. Using the INSECURE mode for connection!");
client.setInsecure();
}
else if(options.caCert != "") client.setTrustAnchors(new X509List(options.caCert.c_str()));
else client.setFingerprint(options.fingerprint.c_str());
#elif defined(ESP32)
if(options.caCert == "") {
Expand All @@ -31,7 +30,7 @@ Response fetch(const char* url, RequestOptions options) {
// Connecting to server.
while(!client.connect(parsedUrl.host.c_str(), parsedUrl.port)) {
delay(1000);
// Serial.print(".");
Serial.print(".");
}

// Forming request.
Expand All @@ -43,8 +42,8 @@ Response fetch(const char* url, RequestOptions options) {
"Connection: " + options.headers.connection + "\r\n\r\n" +
options.body + "\r\n\r\n";

// Serial.println("Request is: ");
// Serial.println(request);
Serial.println("Request is: ");
Serial.println(request);

// Sending request.
client.print(request);
Expand All @@ -67,9 +66,9 @@ Response fetch(const char* url, RequestOptions options) {
if(line == "\r") break;
}

// Serial.println("-----HEADERS START-----");
// Serial.println(response.headers.text);
// Serial.println("-----HEADERS END-----");
Serial.println("-----HEADERS START-----");
Serial.println(response.headers.text);
Serial.println("-----HEADERS END-----");

// Getting response body.
while(client.available()) {
Expand Down Expand Up @@ -108,9 +107,6 @@ String operator+(String str, Body body) {
return str + body.text();
}

// Fetch::Fetch(const char* url, RequestOptions options) :
// _url(url), _options(options) {}

Response::Response(): ok(false), status(200), statusText("OK"),
redirected(false), type(""), headers(ResponseHeaders()), body("") {}

Expand Down
9 changes: 0 additions & 9 deletions src/Fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,6 @@ class RequestOptions {
RequestOptions();
};

// class Fetch {
// private:
// const char* _url;
// RequestOptions _options;
// public:
// Fetch();
// Fetch(const char* url, RequestOptions options);
// };

Response fetch(const char* url, RequestOptions options);

#endif

0 comments on commit f58b645

Please sign in to comment.