Layer: L7 Application ยท Build flags: none (core features only)
What this example teaches
Handlers can queue extra response headers and cookies before they send. The queued headers live in a fixed per-connection buffer, are injected into the next send() / send_empty() / redirect() on that slot, and are cleared automatically at the start of the next request.
The three calls.
server.add_response_header(slot_id, "X-Api-Version", "1.2.5");
server.set_cookie(slot_id, "session", "abc123", "Path=/; HttpOnly; Max-Age=3600");
server.clear_response_headers(slot_id);
The /cleared route queues a header then calls clear_response_headers() before sending, so the header does not appear - useful when a later code path decides the earlier header was wrong. Oversized headers are dropped whole (never truncated mid-line), preserving a valid response.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L7-Application/ResponseHeaders/ResponseHeaders.ino
curl -D - http://<ip>/headers # X-Api-Version + Set-Cookie present
curl -D - http://<ip>/cleared # no X-Scratch (cleared before send)
Annotated source
The complete sketch (ResponseHeaders.ino), reproduced verbatim with added explanatory comments:
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
void handle_headers(uint8_t slot_id,
HttpReq *req)
{
(void)req;
server.
set_cookie(slot_id,
"session",
"abc123",
"Path=/; HttpOnly; Max-Age=3600");
server.
send(slot_id, 200,
"text/plain",
"custom header + cookie attached");
}
void handle_cleared(uint8_t slot_id,
HttpReq *req)
{
(void)req;
server.
send(slot_id, 200,
"text/plain",
"headers were cleared before send");
}
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
int32_t result = server.
begin(80);
if (result < 0)
{
Serial.printf("begin() failed (error %d)\n", result);
return;
}
Serial.println("Server started on port 80");
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
void clear_response_headers(uint8_t slot_id)
Discard any headers/cookies queued for this slot.
void add_response_header(uint8_t slot_id, const char *name, const char *value)
Queue a custom response header for the next send on this slot.
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
void set_cookie(uint8_t slot_id, const char *name, const char *value, const char *attrs=nullptr)
Queue a Set-Cookie response header for the next send on this slot.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.