Layer: L6 Presentation ยท Build flags: none (core features only)
What this example teaches
This shows the three request-data accessors working together: http_get_form() for application/x-www-form-urlencoded body fields, http_get_query() for query-string parameters, and http_get_header() for request headers - all allocation-free, reading into caller-supplied buffers.
Form fields on demand. http_get_form() parses a named field out of the body into your buffer and returns whether it was found. It is gated on the Content-Type: application/x-www-form-urlencoded header and returns the raw (un-decoded) value, matching http_get_query():
char name[48], email[64];
bool have_email =
http_get_form(req,
"email", email,
sizeof(email));
bool http_get_form(const HttpReq *req, const char *key, char *out, size_t out_size)
Look up an application/x-www-form-urlencoded body field by name.
Mixing sources. A ?debug=1 query parameter (read with http_get_query) switches on extra output that also reflects the User-Agent header (read with http_get_header) - so one request reads from the body, the query string, and a header at once:
if (debug && strcmp(debug, "1") == 0) {
}
const char * http_get_header(const HttpReq *req, const char *key)
Look up a header value by name (case-insensitive).
const char * http_get_query(const HttpReq *req, const char *key)
Look up a query parameter value by name (case-sensitive).
Missing both fields yields a 400; otherwise the handler echoes them back as JSON built with snprintf.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L6-Presentation/FormParams/FormParams.ino
curl -X POST http://<ip>/form -d "name=ada&email=ada@example.com"
curl -X POST "http://<ip>/form?debug=1" -d "name=ada&email=ada@example.com"
Annotated source
The complete sketch (FormParams.ino), reproduced verbatim with added explanatory comments:
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
void handle_form(uint8_t slot_id,
HttpReq *req)
{
char name[48];
char email[64];
bool have_email =
http_get_form(req,
"email", email,
sizeof(email));
if (!have_name && !have_email)
{
server.
send(slot_id, 400,
"text/plain",
"expected urlencoded body with name= / email=");
return;
}
char body[256];
if (debug && strcmp(debug, "1") == 0)
{
snprintf(body, sizeof(body), "{\"name\":\"%s\",\"email\":\"%s\",\"ua\":\"%s\"}", have_name ? name : "",
have_email ? email : "", ua ? ua : "");
}
else
{
snprintf(body, sizeof(body), "{\"name\":\"%s\",\"email\":\"%s\"}", have_name ? name : "",
have_email ? email : "");
}
server.
send(slot_id, 200,
"application/json", body);
}
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 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.
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_POST
Non-idempotent create / action.
Fully-parsed HTTP/1.1 request.