Skip to content

Commit 015a133

Browse files
author
MA
committed
Response object is populated with important data.
1 parent 9d31c2d commit 015a133

File tree

4 files changed

+124
-37
lines changed

4 files changed

+124
-37
lines changed

README.md

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
# Arduino Fetch
2-
High level HTTP Request Library that gives a javascript fetch like API.
2+
Arduino fetch is a high level HTTP Request Library that gives you a javascript fetch like API.
3+
4+
```js
5+
ResponseOptions options;
6+
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";
8+
options.headers.contentType = "application/json";
9+
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";
10+
11+
/** In JSON, this would look like:
12+
* options = {
13+
* method: "POST",
14+
* fingerprint: "DC 78 3C 09 3A 78 E3 A0 BA A9 C5 4F 7A A0 87 6F 89 01 71 4C",
15+
* headers: { contentType: "application/json" },
16+
* body: "{\"email\": \"[email protected]\", \"password\": \"test:80\"}",
17+
* }
18+
*/
19+
20+
Response response = fetch("https://api.grandeur.tech/auth/login/?apiKey=grandeurkywxmoy914080rxf9dh05n7e", options);
21+
```
322

423
# Include
524

@@ -20,48 +39,66 @@ RequestOptions options;
2039
/**
2140
{
2241
method: "GET" || "POST" || "HEAD" || "PUT" || "DELETE",
23-
headers: { "Content-Type": "application/urlencoded", "Content-Length": Automatic, "Host: FromURL, "User-Agent": "arduino-fetch", "Cookie", "Accept": "* /*", "Connection": "close", "Transfer-Encoding": "chunked" },
24-
body: {},
42+
headers: { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": Automatic, "Host: FromURL, "User-Agent": "arduino-fetch", "Cookie": "", "Accept": "* /*", "Connection": "close", "Transfer-Encoding": "chunked" },
43+
body: "",
2544
redirect: "follow" || "manual", "error",
2645
follow: Integer,
2746
2847
}
2948
*/
3049

31-
options["method"] = "POST";
32-
options["headers"]["Content-Type"] = "application/json";
33-
options["body"]["email"] = email;
34-
options["body"]["password"] = password;
50+
ResponseOptions options;
51+
options.method = "POST";
52+
options.body = "email=EMAIL&password=PASSWORD";
3553
```
36-
## Request Body
3754

55+
<!-- ```cpp
56+
ResponseOptions options;
57+
options["method"] = "POST";
58+
options["body"] = "email=EMAIL&password=PASSWORD";
59+
``` -->
60+
61+
## Request Body
62+
<!--
3863
```cpp
3964
MultipartFormBody body;
4065
URLEncodedBody body;
4166
JSONBody body;
4267
4368
body["email"] = email;
4469
body["password"] = password;
70+
``` -->
71+
```cpp
72+
String body;
73+
// application/x-www-form-urlencoded
74+
body = "email=EMAIL&password=PASSWORD";
75+
// application/json
76+
body = "{\"email\":\"EMAIL\", \"password\"=\"PASSWORD\"}";
4577
```
46-
4778
## Request Headers
4879

49-
```cpp
80+
<!-- ```cpp
5081
Headers headers;
5182
5283
headers["Content-Type"] = "application/json";
53-
```
84+
``` -->
85+
```cpp
86+
Headers headers;
5487

88+
headers.contentType = "application/json";
89+
headers.connection = "keep-alive";
90+
headers.cookie = "abc=def,ghi=jkl";
91+
```
5592
# Response
5693

5794
```cpp
5895
Response response;
5996

60-
JSONBody data = response.json();
61-
const char* data = response.text();
62-
const Blob = response.blob();
63-
const FormData = response.formData();
64-
unsigned int[] array = response.arrayBuffer();
97+
String data = response.text();
98+
JSONBody data = response.json(); // Not yet supported.
99+
const Blob = response.blob(); // Not yet supported.
100+
const FormData = response.formData(); // Not yet supported.
101+
unsigned int[] array = response.arrayBuffer(); // Not yet supported.
65102

66103
bool ok = response.ok;
67104
int status = response.status;

examples/post/post.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ void setup() {
1616
options.fingerprint = FINGERPRINT;
1717

1818
Response response = fetch("https://api.grandeur.tech/auth/login/?apiKey=grandeurkywxmoy914080rxf9dh05n7e", options);
19-
20-
Serial.println(response.text());
19+
20+
Serial.println(response);
21+
Serial.printf("Connection Header: \"%s\"\n", response.headers.get("Content-Type").c_str());
22+
Serial.printf("Connection Header: \"%s\"\n", response.headers.get("Connection").c_str());
2123
}
2224

2325
void loop() {

src/ArduinoFetch.cpp

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Response fetch(const char* url, RequestOptions options) {
1313

1414
// Set fingerprint if https.
1515
if(parsedUrl.scheme == "https") {
16-
if(options.fingerprint == "\0") Serial.println("[Error] Provide fingerprint for HTTPS.");
16+
if(options.fingerprint == "") Serial.println("[Error] Provide fingerprint for HTTPS.");
1717
else client.setFingerprint(options.fingerprint.c_str());
1818
}
1919

@@ -39,30 +39,45 @@ Response fetch(const char* url, RequestOptions options) {
3939
client.print(request);
4040

4141
// Getting response headers.
42-
String headers = "";
43-
while(client.connected()) {
42+
Response response;
43+
for(int nLine = 1; client.connected(); nLine++) {
44+
// Reading headers line by line.
4445
String line = client.readStringUntil('\n');
45-
headers += line;
46+
// Parse status and statusText from line 1.
47+
if(nLine == 1) {
48+
response.status = line.substring(line.indexOf(" ")).substring(0, line.indexOf(" ")).toInt();
49+
response.statusText = line.substring(line.indexOf(String(response.status)) + 4);
50+
response.statusText.trim();
51+
continue;
52+
}
53+
54+
response.headers.text += line + "\n";
55+
// If headers end, move on.
4656
if(line == "\r") break;
4757
}
4858

4959
// Serial.println("-----HEADERS START-----");
50-
// Serial.println(headers);
60+
// Serial.println(response.headers.text);
5161
// Serial.println("-----HEADERS END-----");
5262

5363
// Getting response body.
54-
String body = "";
5564
while(client.available()) {
56-
body += client.readStringUntil('\n');
57-
body += "\n";
65+
response.body += client.readStringUntil('\n');
5866
}
5967

60-
return Response(body);
68+
return response;
6169
}
6270

6371
Headers::Headers(): contentType("application/x-www-form-urlencoded"),
64-
contentLength(-1), userAgent("arduino-fetch"), cookie("\0"),
65-
accept("*/*"), connection("close"), transferEncoding("\0") {}
72+
contentLength(-1), userAgent("arduino-fetch"), cookie(""),
73+
accept("*/*"), connection("close"), transferEncoding("") {}
74+
75+
ResponseHeaders::ResponseHeaders(): text("") {}
76+
77+
String ResponseHeaders::get(String headerName) {
78+
String tillEnd = this->text.substring(this->text.lastIndexOf(headerName + ": "));
79+
return tillEnd.substring(tillEnd.indexOf(" ") + 1, tillEnd.indexOf("\n") - 1);
80+
}
6681

6782
Body::Body(): _text("") {}
6883

@@ -85,11 +100,27 @@ String operator+(String str, Body body) {
85100
// ArduinoFetch::ArduinoFetch(const char* url, RequestOptions options) :
86101
// _url(url), _options(options) {}
87102

88-
Response::Response(String body): _text(body) {}
103+
Response::Response(): ok(false), status(200), statusText("OK"),
104+
redirected(false), type(""), headers(ResponseHeaders()), body("") {}
89105

90106
String Response::text() {
91-
return this->_text;
107+
return this->body;
108+
}
109+
110+
size_t Response::printTo(Print& p) const {
111+
size_t r = 0;
112+
113+
r += p.printf(
114+
(String("{") +
115+
"\n\t\"ok\": %d" +
116+
"\n\t\"status\": %d" +
117+
"\n\t\"statusText\": \"%s\"" +
118+
"\n\t\"body\": \"%s\"" +
119+
"\n}").c_str(),
120+
ok, status, statusText.c_str(), body.c_str());
121+
122+
return r;
92123
}
93124

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

src/ArduinoFetch.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,30 @@ String operator+(String str, Body body);
4848
// Request();
4949
// };
5050

51-
class Response {
51+
class ResponseHeaders {
5252
private:
53-
String _text;
5453
public:
55-
Response();
56-
Response(String body);
57-
String text();
54+
String text;
55+
56+
ResponseHeaders();
57+
String get(String headerName);
58+
};
59+
60+
class Response: public Printable {
61+
private:
62+
public:
63+
bool ok;
64+
int status;
65+
String statusText;
66+
bool redirected;
67+
String type;
68+
ResponseHeaders headers;
69+
String body;
70+
71+
Response();
72+
String text();
73+
74+
size_t printTo(Print& p) const;
5875
};
5976

6077
class RequestOptions {

0 commit comments

Comments
 (0)