Layer: L6 Presentation ยท Build flags: none (core; multipart is on by default)
What this example teaches
This parses a multipart/form-data POST body (RFC 7578) without allocation: pc_multipart_parse() splits the in-buffer body into parts, and pc_multipart_get_field() returns a named text field. A test form is served at /.
In-place, bounded parsing. The whole body must fit in BODY_BUF_SIZE (there is no streaming), so this is for small form fields and tiny uploads. The parser populates a Multipart struct that indexes into the existing body buffer rather than copying:
server.send(id, 400, "text/plain", "expected multipart/form-data (and within BODY_BUF_SIZE)");
return;
}
const char * pc_multipart_get_field(const Multipart *mp, const char *field)
Look up a field value across all parsed parts by name.
bool pc_multipart_parse(HttpReq *req, Multipart *mp)
Parse the body of req as multipart/form-data.
Container for all parsed parts of a multipart body.
mp.part_count tells you how many parts were found. For large/streamed uploads straight to a file, see FileUpload.
The HTML form at / posts a name field and a file input so you can exercise it from a browser.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L6-Presentation/Multipart/Multipart.ino
Flash, then browse to http://<ip>/ and submit the form.
Annotated source
The complete sketch (Multipart.ino), reproduced verbatim with added explanatory comments:
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char FORM[] = "<!doctype html><meta charset=utf-8><title>upload</title>"
"<form method=POST action=/upload enctype=multipart/form-data>"
"<input name=name placeholder=name> "
"<input type=file name=file> <button>upload</button></form>";
void handle_upload(uint8_t
id,
HttpReq *req)
{
{
server.
send(
id, 400,
"text/plain",
"expected multipart/form-data (and within BODY_BUF_SIZE)");
return;
}
char out[160];
snprintf(out,
sizeof(out),
"parsed %d part(s); field 'name' = %s", mp.
part_count, name ? name :
"(absent)");
server.
send(
id, 200,
"text/plain", out);
}
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));
}
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.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.
int part_count
Number of valid entries in parts[].