ESP32 WebServer Library – version()

Home / References / ESP32 Library / WebServer Library

Description

The version() method is used within a request handler function to determine the HTTP protocol version specified by the web client (like a browser or another application) in its request to the ESP32 web server.

When a client sends an HTTP request, the first line typically includes the method (GET, POST, etc.), the URI, and the HTTP version it supports (e.g., “HTTP/1.1” or “HTTP/1.0”). The version() method allows your server code to access this specific piece of information from the parsed request.

This can be useful for logging request details, debugging, or understanding the capabilities of the clients interacting with your ESP32 server.


Syntax and Usage

The version() method is called on the WebServer object instance within the function handling the client’s request. It does not take any arguments.

String clientHttpVersion = server.version();
  • Without Arguments: This is the only way to use this method. It directly accesses the stored HTTP version string associated with the current 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 Argument.


Return Value

The method returns a String object containing the HTTP version string as sent by the client in the request line. Common return values include:

  • "HTTP/1.1"
  • "HTTP/1.0"

The specific value depends on what the client specified in its request header.


Example Codes

Example: Displaying the Client’s HTTP Version

This example sets up a basic ESP32 web server. When a client connects to the root URL (‘/’), the server retrieves the client’s HTTP version using server.version(), prints it to the Serial Monitor, and sends it back to the client as part of the response.

How to use this code:

  1. Replace "YOUR_WIFI_SSID" and "YOUR_WIFI_PASSWORD" with your network details.
  2. Upload the sketch to your ESP32.
  3. Open the Serial Monitor in the Arduino IDE (set baud rate to 115200).
  4. After the ESP32 connects to WiFi and the server starts, copy the IP address shown in the Serial Monitor.
  5. Open a web browser on a device connected to the same network and go to http://<ESP32_IP>/ (using the copied IP address).
  6. Check the Serial Monitor; it will print the HTTP version detected from your browser’s request.
  7. The browser will display a message confirming the detected HTTP version.
/*
 * Author: Avant Maker
 * Date: April 30, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * This example demonstrates how to use ESP32 WebServer Library's 
 * version() method to determine the HTTP protocol version specified
 * by the web client (like a browser or another application) in its
 * request to the ESP32 web server.
 *
 * 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);

// Handler function for the root URL ("/")
void handleRoot() {
  // Get the HTTP version string from the request
  String httpVersion = server.version();

  // Print the version to the Serial Monitor
  Serial.print("Request received. Client HTTP Version: ");
  Serial.println(httpVersion);

  // Prepare the response message for the client
  String responseMessage = "AvantMaker Demo - Hello from ESP32!\n";
  responseMessage += "Your request used HTTP version: ";
  responseMessage += httpVersion;

  // Send the response
  server.send(200, "text/plain", responseMessage);
}

void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup() {
  Serial.begin(115200);
  delay(100); // Wait for serial

  Serial.println();
  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());

  // Set up the handler for the root path
  server.on("/", HTTP_GET, handleRoot);

  // Set up the handler for pages that are not found
  server.onNotFound(handleNotFound);

  // Start the server
  server.begin();
  Serial.println("HTTP server started. Open browser to http://" + WiFi.localIP().toString());
}

void loop() {
  // Listen for incoming client connections and handle them
  server.handleClient();
}
error: Content is protected !!