Skip to content

Commit 9d31c2d

Browse files
author
MA
committed
Get and Post examples are tested with fingerprints.
1 parent 0c35911 commit 9d31c2d

File tree

6 files changed

+89
-46
lines changed

6 files changed

+89
-46
lines changed

examples/get/get.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#include "recipes/WiFi.h"
22
#include "ArduinoFetch.h"
33

4-
#define SSID WIFISSID
5-
#define PASSPHRASE WIFIPASSPHRASE
4+
#define SSID YourWiFiSSID
5+
#define PASSPHRASE YourWiFiPassphrase
6+
#define FINGERPRINT "96 84 07 DF 0B 1C F6 58 14 DF D7 33 35 57 51 9B 15 4D 8C E7"
7+
68

79
void setup() {
810
Serial.begin(9200);
911
connectWiFi(SSID, PASSPHRASE);
1012

1113
RequestOptions options;
1214
options.method = "GET";
15+
options.fingerprint = FINGERPRINT;
1316

14-
Response response = fetch("https://api.grandeur.tech/", options);
17+
Response response = fetch("https://api.github.com/", options);
1518

1619
Serial.println(response.text());
1720
}

examples/post/get.ino renamed to examples/post/post.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "recipes/WiFi.h"
22
#include "ArduinoFetch.h"
33

4-
#define SSID WIFISSID
5-
#define PASSPHRASE WIFIPASSPHRASE
4+
#define SSID YourWiFiSSID
5+
#define PASSPHRASE YourWiFiPassphrase
6+
#define FINGERPRINT "DC 78 3C 09 3A 78 E3 A0 BA A9 C5 4F 7A A0 87 6F 89 01 71 4C"
67

78
void setup() {
89
Serial.begin(9200);
@@ -12,6 +13,7 @@ void setup() {
1213
options.method = "POST";
1314
options.headers.contentType = "application/json";
1415
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";
16+
options.fingerprint = FINGERPRINT;
1517

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

src/ArduinoFetch.cpp

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,42 @@
11
#include "ArduinoFetch.h"
22
#include <WiFiClientSecure.h>
3-
#include <LCBUrl.h>
43

54
// ArduinoFetch arduinoFetch;
65

7-
const char fingerprint[] PROGMEM = "DC 78 3C 09 3A 78 E3 A0 BA A9 C5 4F 7A A0 87 6F 89 01 71 4C";
8-
96
Response fetch(const char* url, RequestOptions options) {
107
// Parsing URL.
11-
LCBUrl lcbUrl;
12-
lcbUrl.setUrl(url);
13-
String scheme = lcbUrl.getScheme();
14-
String host = lcbUrl.getHost();
15-
String path = "/" + lcbUrl.getPath();
16-
String afterPath = lcbUrl.getAfterPath();
17-
unsigned int port = lcbUrl.getPort();
18-
19-
Serial.printf("scheme: %s, host: %s, path: %s, pathSegment: %s, port: %u\n", scheme.c_str(), host.c_str(), path.c_str(), afterPath.c_str(), port);
8+
Url parsedUrl = parseUrl(url);
209

2110
WiFiClientSecure client;
2211
// Retry every 15 seconds.
2312
client.setTimeout(15000);
24-
// Set fingerprint if https.
25-
if(scheme == "https") client.setFingerprint(fingerprint);
2613

27-
delay(1000);
14+
// Set fingerprint if https.
15+
if(parsedUrl.scheme == "https") {
16+
if(options.fingerprint == "\0") Serial.println("[Error] Provide fingerprint for HTTPS.");
17+
else client.setFingerprint(options.fingerprint.c_str());
18+
}
2819

2920
// Connecting to server.
30-
while(!client.connect(host, port)) {
21+
while(!client.connect(parsedUrl.host, parsedUrl.port)) {
3122
delay(1000);
32-
Serial.print(".");
23+
// Serial.print(".");
3324
}
3425

35-
Serial.println("Request is: ");
36-
Serial.println(
37-
options.method + " " + path + afterPath + " HTTP/1.1\r\n" +
38-
"Host: " + host + "\r\n" +
26+
// Forming request.
27+
String request =
28+
options.method + " " + parsedUrl.path + parsedUrl.afterPath + " HTTP/1.1\r\n" +
29+
"Host: " + parsedUrl.host + "\r\n" +
30+
"User-Agent: " + options.headers.userAgent + "\r\n" +
3931
"Content-Type: " + options.headers.contentType + "\r\n" +
40-
"Connection: close\r\n\r\n" +
41-
options.body + "\r\n\r\n"
42-
);
32+
"Connection: " + options.headers.connection + "\r\n\r\n" +
33+
options.body + "\r\n\r\n";
34+
35+
// Serial.println("Request is: ");
36+
// Serial.println(request);
4337

4438
// Sending request.
45-
client.print(
46-
options.method + " " + path + afterPath + " HTTP/1.1\r\n" +
47-
"Host: " + host + "\r\n" +
48-
"Content-Type: " + options.headers.contentType + "\r\n" +
49-
"Connection: close\r\n\r\n" +
50-
options.body + "\r\n\r\n"
51-
);
39+
client.print(request);
5240

5341
// Getting response headers.
5442
String headers = "";
@@ -58,9 +46,9 @@ Response fetch(const char* url, RequestOptions options) {
5846
if(line == "\r") break;
5947
}
6048

61-
Serial.println("-----HEADERS START-----");
62-
Serial.println(headers);
63-
Serial.println("-----HEADERS END-----");
49+
// Serial.println("-----HEADERS START-----");
50+
// Serial.println(headers);
51+
// Serial.println("-----HEADERS END-----");
6452

6553
// Getting response body.
6654
String body = "";
@@ -73,7 +61,7 @@ Response fetch(const char* url, RequestOptions options) {
7361
}
7462

7563
Headers::Headers(): contentType("application/x-www-form-urlencoded"),
76-
contentLength(-1), host("\0"), userAgent("arduino-fetch"), cookie("\0"),
64+
contentLength(-1), userAgent("arduino-fetch"), cookie("\0"),
7765
accept("*/*"), connection("close"), transferEncoding("\0") {}
7866

7967
Body::Body(): _text("") {}
@@ -103,5 +91,5 @@ String Response::text() {
10391
return this->_text;
10492
}
10593

106-
RequestOptions::RequestOptions(): method("GET"), headers(Headers()), body(Body()) {}
94+
RequestOptions::RequestOptions(): method("GET"), headers(Headers()), body(Body()), fingerprint("\0") {}
10795

src/ArduinoFetch.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
#include "utils/FormData.h"
2-
#include "utils/URLEncoded.h"
3-
#include "utils/JSON.h"
1+
// #include "utils/FormData.h"
2+
// #include "utils/URLEncoded.h"
3+
// #include "utils/JSON.h"
4+
#include "utils/Url/Url.h"
45
#include <Arduino.h>
56

7+
#ifndef FETCH_H_
8+
#define FETCH_H_
9+
610
class Headers {
711
private:
812
public:
913
String contentType;
1014
unsigned long contentLength;
11-
String host;
1215
String userAgent;
1316
String cookie;
1417
String accept;
@@ -60,6 +63,7 @@ class RequestOptions {
6063
String method;
6164
Headers headers;
6265
Body body;
66+
String fingerprint;
6367
RequestOptions();
6468
};
6569

@@ -72,4 +76,6 @@ class RequestOptions {
7276
// ArduinoFetch(const char* url, RequestOptions options);
7377
// };
7478

75-
Response fetch(const char* url, RequestOptions options);
79+
Response fetch(const char* url, RequestOptions options);
80+
81+
#endif

src/utils/Url/Url.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "Url.h"
2+
3+
Url::Url() {}
4+
5+
Url parseUrl(const char* url) {
6+
// Parsing URL.
7+
LCBUrl lcbUrl;
8+
lcbUrl.setUrl(url);
9+
Url parsed;
10+
parsed.scheme = lcbUrl.getScheme();
11+
parsed.username = lcbUrl.getUserName();
12+
parsed.password = lcbUrl.getPassword();
13+
parsed.host = lcbUrl.getHost();
14+
parsed.path = "/" + lcbUrl.getPath();
15+
parsed.afterPath = lcbUrl.getAfterPath();
16+
parsed.query = lcbUrl.getQuery();
17+
parsed.fragment = lcbUrl.getFragment();
18+
parsed.port = lcbUrl.getPort();
19+
20+
return parsed;
21+
}

src/utils/Url/Url.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <LCBUrl.h>
2+
3+
#ifndef URL_H_
4+
#define URL_H_
5+
6+
class Url {
7+
private:
8+
public:
9+
String scheme;
10+
String username;
11+
String password;
12+
String host;
13+
String path;
14+
String afterPath;
15+
String query;
16+
String fragment;
17+
uint port;
18+
Url();
19+
};
20+
21+
Url parseUrl(const char* url);
22+
23+
#endif

0 commit comments

Comments
 (0)