Making Requests¶
Every HTTP method on ESP32HTTPClient returns a RestRequest object. You can chain .query(), .body(), and .getBody() calls on it. The request is dispatched automatically when the chain goes out of scope (i.e., at the end of the statement).
GET¶
Path Parameters¶
Use .path(key, value) to dynamically substitute {placeholder} parameters in the URL path. Supports int, float, double, long, bool, and const char*.
Query Parameters¶
Use .query(key, value) to append URL parameters. Supports int, float, double, long, bool, and const char*.
// Produces: GET /users?page=2&limit=20&search=pedro
client.get("/users")
.query("page", 2)
.query("limit", 20)
.query("search", "pedro");
POST¶
Use .body(key, value) to build the JSON body. The Content-Type is automatically set to application/json.
// Body: {"title": "foo", "body": "bar", "userId": 1}
int newId;
client.post("/posts")
.body("title", "foo")
.body("body", "bar")
.body("userId", 1)
.getBody("id", &newId);
// Check creation status
if (client.getStatusCode() == 201) {
Serial.printf("Created with ID: %d\n", newId);
}
PUT / update¶
put() and update() are aliases for the same HTTP PUT method:
client.put("/lights/1").body("state", "OFF");
// or equivalently:
client.update("/lights/1").body("state", "OFF");
PATCH¶
DELETE¶
DELETE requests can optionally include a body:
Supported Value Types for .path(), .query(), and .body()¶
| C Type | JSON output | Example |
|---|---|---|
const char* / char* |
"string" (quoted) |
.body("name", "Pedro") |
String |
"string" (quoted) |
.body("title", title) |
int / unsigned int |
42 |
.body("age", 21) |
long / unsigned long |
1721000000 |
.body("ts", unixTime) |
long long / uint64_t |
12345678901234 |
.body("id", largeId) |
float |
24.5 |
.body("temp", 24.5f) |
double |
3.14159265 |
.body("pi", 3.14159265) |
bool |
true / false |
.body("active", true) |
Checking the HTTP Status Code¶
After any request, use getStatusCode() to inspect the result:
client.get("/api/status");
int code = client.getStatusCode();
if (code == 200) {
Serial.println("OK");
} else if (code < 0) {
Serial.println("Connection error");
} else {
Serial.printf("HTTP error: %d\n", code);
}
Status code availability
getStatusCode() always returns the code from the last completed request. Negative values indicate a network-level error (e.g., no connection).