ESP32 WebServer Library – args()

Home / References / ESP32 Library / WebServer Library

Description

The args() method is a member function of the ESP32 WebServer class. It is used within request handler functions to determine the total number of arguments (also known as parameters or query string variables) that were sent by the client in an HTTP request. This method counts arguments received via both GET (typically in the URL’s query string) and POST (typically in the request body from form submissions) methods.

Knowing the number of arguments is often the first step before iterating through them using methods like argName() and arg() to process the data sent by the client.


Syntax and Usage

To use the args() method, you simply call it on your instantiated WebServer object. It does not take any parameters.

#include <WebServer.h>

// Assuming 'server' is your WebServer object instance
// WebServer server(80);

void handleClientRequest() {
  // Get the total number of arguments received
  size_t argumentCount = server.args();

  // Now you can use argumentCount, for example, to loop through arguments
  Serial.print("Number of arguments received: ");
  Serial.println(argumentCount);
}
  • server.args(): Call this method on your active WebServer instance (e.g., server). It returns the count of arguments associated with the current client request being processed.

For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.


Argument(s)

This method does not require any arguments.


Return Value

The args() method returns a single value:

  • Return Type: size_t. This is an unsigned integer type commonly used in C++ to represent sizes and counts.
  • Value: The total number of arguments parsed from the incoming HTTP request. This includes arguments from the query string (after the ‘?’ in the URL for GET requests) and the request body (for POST requests with content type ‘application/x-www-form-urlencoded’). If no arguments are present in the request, the method returns 0.

Example Codes

This example demonstrates how to set up a basic ESP32 web server that listens for requests on the path /submit. When a request is received (either GET or POST), the handler function uses server.args() to count the number of submitted arguments and prints this count to the Serial Monitor. It then proceeds to print the name and value of each argument.

To test this code:

  1. Upload the sketch to your ESP32 board after replacing the placeholder Wi-Fi credentials.
  2. Open the Serial Monitor to see the ESP32’s IP address once it connects to Wi-Fi.
  3. Open a web browser on a device connected to the same network.
  4. Navigate to an address like: http://<ESP32_IP>/submit?sensor=temperature&value=25.5&unit=C (replace <ESP32_IP> with the actual IP address).
  5. Observe the Serial Monitor output. It should report “Number of arguments received: 3” and then list the names and values (sensor=temperature, value=25.5, unit=C). You can change the arguments in the URL to see the count change.
/*
 * Author: Avant Maker
 * Date: April 21, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * This example demonstrates how to use ESP32 WebServer Library's 
 * args() method to count the number of submitted arguments and
 * prints this count to the Serial Monitor.
 *
 * Code Source:
 * This example code is sourced from the Comprehensive Guide
 * to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
 * For additional code examples and in-depth documentation related to
 * the ESP32 Arduino Core Library, please visit:
 *
 * https://avantmaker.com/home/all-about-esp32-arduino-core-library/
 *
 * AvantMaker.com, your premier destination for all things
 * DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
 * to empowering makers, learners, and enthusiasts with
 * the resources they need to bring their innovative ideas to life.
 */

#include <WiFi.h>
#include <WebServer.h>

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";


// Create WebServer object on port 80
WebServer server(80);

// Function to handle requests to /submit
void handleSubmit() {
  // Get the number of arguments
  size_t argCount = server.args();

  Serial.print("Request received for /submit. Number of arguments: ");
  Serial.println(argCount);

  String response = "<html><body><h1>Arguments Received: ";
  response += argCount;
  response += "</h1>";

  // If there are arguments, list them
  if (argCount > 0) {
    response += "<ul>";
    for (int i = 0; i < argCount; i++) {
      String argName = server.argName(i);
      String argValue = server.arg(i);
      Serial.print("Arg ");
      Serial.print(i);
      Serial.print(" - Name: ");
      Serial.print(argName);
      Serial.print(", Value: ");
      Serial.println(argValue);

      response += "<li>";
      response += argName;
      response += " = ";
      response += argValue;
      response += "</li>";
    }
    response += "</ul>";
  } else {
      response += "<p>No arguments were submitted.</p>";
  }

  response += "</body></html>";

  // Send the response back to the client
  server.send(200, "text/html", response);
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Starting ESP32 Web Server...");

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" Connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Define request handler for /submit path
  server.on("/submit", handleSubmit); // Handles both GET and POST by default

  // Start the server
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  // Handle client requests
  server.handleClient();
}
error: Content is protected !!