ESP32 WebServer Library – argName()

Home / References / ESP32 Library / WebServer Library

Description

The argName() method is part of the ESP32 WebServer library for Arduino. It is used to retrieve the name (or key) of a specific argument that was passed in an HTTP request (either GET or POST) to the ESP32 web server. Arguments are identified by their index number, starting from zero. This method is essential for parsing incoming request parameters, allowing your ESP32 application to understand and react to the specific data sent by a client.


Syntax and Usage

The argName() method is called on an instance of the WebServer class. It requires a single argument: the index of the parameter whose name you want to retrieve.

// Assuming 'server' is an instance of the WebServer class
String argumentName = server.argName(index);
  • server: Your instance of the WebServer object.
  • index: An integer representing the zero-based index of the argument you want to query. The index ranges from 0 to server.args() - 1.

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 requires the following argument:

  • index (int): The zero-based index of the request argument whose name should be returned. For example, the first argument received has an index of 0, the second has an index of 1, and so on. The total number of arguments can be obtained using the args() method.

Return Value

The method returns a String object containing the name (key) of the argument found at the specified index. If the provided index is out of bounds (i.e., less than 0 or greater than or equal to the total number of arguments), it returns an empty String.


Example Codes

The following example demonstrates how to set up a simple web server on the ESP32. When a client connects to the root URL (/) and provides query parameters (e.g., http://<ESP32_IP>/?sensor=temperature&value=23.5), the server iterates through all received arguments, retrieves their names using argName() and their values using arg(), and displays them on the response page.

How to use the example:

  1. Replace "YOUR_WIFI_SSID" and "YOUR_WIFI_PASSWORD" with your actual Wi-Fi credentials.
  2. Upload the code to your ESP32 board.
  3. Open the Serial Monitor to find the IP address assigned to the ESP32.
  4. Open a web browser and navigate to the ESP32’s IP address, adding some query parameters. For example: http://<ESP32_IP>/?city=Toronto&unit=celsius&id=123
  5. The browser will display a page listing the index, name, and value of each argument received (city, unit, id).
/*
 * Author: Avant Maker
 * Date: April 21, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * The following example demonstrates how to set up a simple web
 * server on the ESP32. When a client connects to the root URL (/)
 * and provides query parameters
 * (e.g., http://<ESP32_IP>/?sensor=temperature&value=23.5),
 * the server iterates through all received arguments,
 * retrieves their names using argName() and their values using arg(),
 * and displays them on the response page.
 *
 * 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_SSID";          // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD";  // Replace with your Wi-Fi password

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

// Function to handle the root URL
void handleRoot() {
  String html = "<!DOCTYPE html><html><head><title>ESP32 Arguments</title></head><body>";
  html += "<h1>Arguments Received:</h1>";

  int numArgs = server.args(); // Get number of arguments

  if (numArgs == 0) {
    html += "<p>No arguments received.</p>";
  } else {
    html += "<table border='1'><tr><th>Index</th><th>Name</th><th>Value</th></tr>";
    for (int i = 0; i < numArgs; i++) {
      String arg_Name = server.argName(i); // Get argument name by index
      String arg_Value = server.arg(i);    // Get argument value by index
      html += "<tr>";
      html += "<td>" + String(i) + "</td>";
      html += "<td>" + arg_Name + "</td>";
      html += "<td>" + arg_Value + "</td>";
      html += "</tr>";
    }
    html += "</table>";
  }

  html += "</body></html>";
  server.send(200, "text/html", html); // Send the HTML response
}

void setup() {
  Serial.begin(115200);
  Serial.println();

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

  // Define request handler
  server.on("/", HTTP_GET, handleRoot); // Handle GET requests on root path

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

void loop() {
  server.handleClient(); // Handle incoming client requests
  delay(2); // Allow the CPU to switch to other tasks
}

error: Content is protected !!