Skip to content

Commit f58b645

Browse files
committed
ESP32 is tested! Readme.md is corrected.
1 parent d736b83 commit f58b645

File tree

8 files changed

+74
-30
lines changed

8 files changed

+74
-30
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ fetch is a high level HTTP Request Library that gives you a javascript fetch lik
44
```js
55
ResponseOptions options;
66
options.method = "POST";
7-
options.fingerprint = "DC 78 3C 09 3A 78 E3 A0 BA A9 C5 4F 7A A0 87 6F 89 01 71 4C";
7+
// options.fingerprint = "DC 78 3C 09 3A 78 E3 A0 BA A9 C5 4F 7A A0 87 6F 89 01 71 4C";
8+
options.caCert = "";
89
options.headers.contentType = "application/json";
910
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";
1011

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

103104
bool ok = response.ok;
104105
int status = response.status;
105-
const char* statusText = response.statusText;
106+
String statusText = response.statusText;
106107
bool redirected = response.redirected; // Not yet supported.
107-
const char* type = response.type; // Not yet supported.
108-
const char* response.headers.get("content-type");
108+
String type = response.type; // Not yet supported.
109+
String response.headers.get("content-type");
109110
Headers headers = response.headers.raw(); // Not yet supported
110111
const char* contentTypeHeader = headers["content-type"]; // Not yet supported.
111112

examples/esp32/get/get.ino

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,37 @@
33

44
#define SSID YourWiFiSSID
55
#define PASSPHRASE YourWiFiPassphrase
6-
6+
#define CACert "-----BEGIN CERTIFICATE-----\n\
7+
MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n\
8+
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n\
9+
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n\
10+
ZSBFViBSb290IENBMB4XDTA2MTExMDAw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EFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\n\
26+
vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n\
27+
+OkuE6N36B9K\n\
28+
-----END CERTIFICATE-----"
729

830
void setup() {
931
Serial.begin(9600);
1032
connectWiFi(SSID, PASSPHRASE);
1133

1234
RequestOptions options;
1335
options.method = "GET";
36+
options.caCert = CACert;
1437

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

examples/esp32/get/recipes/WiFi.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void connectWiFi(const char* ssid, const char* passphrase) {
1919
while (WiFi.status() != WL_CONNECTED) {
2020
// Print dots in a horizontal line to the Serial, showing the WiFi is trying to connect.
2121
Serial.print(".");
22+
delay(1000);
2223
// Blink LED very fast, showing the WiFi is trying to connect.
2324
// blinkN(10);
2425
}

examples/esp32/post/post.ino

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33

44
#define SSID YourWiFiSSID
55
#define PASSPHRASE YourWiFiPassphrase
6+
#define CACert "-----BEGIN CERTIFICATE-----\n\
7+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n\
8+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n\
9+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n\
10+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n\
11+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n\
12+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n\
13+
h77ct984kI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NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5\n\
29+
ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur\n\
30+
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n\
31+
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n\
32+
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n\
33+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n\
34+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n\
35+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n\
36+
-----END CERTIFICATE-----"
637

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

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

18-
Serial.println(response)
1950
// Printing response.
2051
Serial.println(response);
2152
// Printing respons headers.

examples/esp32/post/recipes/WiFi.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void connectWiFi(const char* ssid, const char* passphrase) {
2020
// Print dots in a horizontal line to the Serial, showing the WiFi is trying to connect.
2121
Serial.print(".");
2222
// Blink LED very fast, showing the WiFi is trying to connect.
23+
delay(1000);
2324
// blinkN(10);
2425
}
2526
// Stop the LED blinking, showing the WiFi is successfully connected.

examples/esp8266/get/get.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "recipes/WiFi.h"
22
#include "Fetch.h"
33

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

88

src/Fetch.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "Fetch.h"
22
#include <WiFiClientSecure.h>
33

4-
// Fetch fetch;
5-
64
Response fetch(const char* url, RequestOptions options) {
75
// Parsing URL.
86
Url parsedUrl = parseUrl(url);
@@ -14,10 +12,11 @@ Response fetch(const char* url, RequestOptions options) {
1412
// Set fingerprint if https.
1513
if(parsedUrl.scheme == "https") {
1614
#ifdef ESP8266
17-
if(options.fingerprint == "") {
18-
Serial.println("[INFO] No fingerprint is provided. Using the INSECURE mode for connection!");
15+
if(options.fingerprint == "" && options.caCert == "") {
16+
Serial.println("[INFO] No fingerprint or caCert is provided. Using the INSECURE mode for connection!");
1917
client.setInsecure();
2018
}
19+
else if(options.caCert != "") client.setTrustAnchors(new X509List(options.caCert.c_str()));
2120
else client.setFingerprint(options.fingerprint.c_str());
2221
#elif defined(ESP32)
2322
if(options.caCert == "") {
@@ -31,7 +30,7 @@ Response fetch(const char* url, RequestOptions options) {
3130
// Connecting to server.
3231
while(!client.connect(parsedUrl.host.c_str(), parsedUrl.port)) {
3332
delay(1000);
34-
// Serial.print(".");
33+
Serial.print(".");
3534
}
3635

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

46-
// Serial.println("Request is: ");
47-
// Serial.println(request);
45+
Serial.println("Request is: ");
46+
Serial.println(request);
4847

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

70-
// Serial.println("-----HEADERS START-----");
71-
// Serial.println(response.headers.text);
72-
// Serial.println("-----HEADERS END-----");
69+
Serial.println("-----HEADERS START-----");
70+
Serial.println(response.headers.text);
71+
Serial.println("-----HEADERS END-----");
7372

7473
// Getting response body.
7574
while(client.available()) {
@@ -108,9 +107,6 @@ String operator+(String str, Body body) {
108107
return str + body.text();
109108
}
110109

111-
// Fetch::Fetch(const char* url, RequestOptions options) :
112-
// _url(url), _options(options) {}
113-
114110
Response::Response(): ok(false), status(200), statusText("OK"),
115111
redirected(false), type(""), headers(ResponseHeaders()), body("") {}
116112

src/Fetch.h

-9
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,6 @@ class RequestOptions {
8787
RequestOptions();
8888
};
8989

90-
// class Fetch {
91-
// private:
92-
// const char* _url;
93-
// RequestOptions _options;
94-
// public:
95-
// Fetch();
96-
// Fetch(const char* url, RequestOptions options);
97-
// };
98-
9990
Response fetch(const char* url, RequestOptions options);
10091

10192
#endif

0 commit comments

Comments
 (0)