-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawArrayJSON.ino
More file actions
65 lines (52 loc) · 1.83 KB
/
Copy pathRawArrayJSON.ino
File metadata and controls
65 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <Arduino.h>
#include <WiFi.h>
#include "ESP32HTTPClient.h"
// WiFi Credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Initialize the RestClient
ESP32HTTPClient client("https://jsonplaceholder.typicode.com");
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void loop() {
String entireArray;
String specificUser;
// Example: GET request binding raw arrays/objects using Arduino String
Serial.println("Send GET request to fetch raw array and raw object...");
// NOTE: Pulling complete arrays or objects into a String results in dynamic memory reallocation.
// Use cautiously on large payloads to prevent heap fragmentation.
// URL: https://jsonplaceholder.typicode.com/users
// Response contains an array of users:
// [
// {
// "id": 1,
// "name": "Leanne Graham",
// "address": { "city": "Gwenborough" }
// },
// { ... }
// ]
// Request 1: Fetch the entire array
client.get("/users")
.getBody("", &entireArray); // Binding to an empty path matches the root array
if (client.getStatusCode() == 200) {
Serial.printf("Raw Array (length: %d):\n%s\n\n", entireArray.length(), entireArray.c_str());
} else {
Serial.printf("Error fetching array: %d\n", client.getStatusCode());
}
// Request 2: Fetch a specific object inside the array
client.get("/users")
.getBody("1", &specificUser); // Extract the second user object entirely
if (client.getStatusCode() == 200) {
Serial.printf("Raw User Object (length: %d):\n%s\n\n", specificUser.length(), specificUser.c_str());
} else {
Serial.printf("Error fetching object: %d\n", client.getStatusCode());
}
delay(15000);
}