webserveruriesp8266esp8266wifi

grab full url from ESP8266WebServer


This question must be a duplicate, but I cannot find the answer. Using the ESP8266WebServer library, there is an uri() method to grab the uri. So in the example: http://example.com/index, it will grab /index, but I would also like to get the example.com. IS there a method for that?


Solution

  • The http(s)://host:port part is not sent to server. The client uses the hostname to resolve the IP address and then the client makes a connection to the IP address on the specified port.

    But HTTP 1.1 has a mandatory Host header in requests to a HTTP server.

    The ESP8266 Arduino ESP8266WebServer library makes the current request's headers accessible on the ESP8266WebServer instance. To get the Host header there is a hostHeader() method.

    Example:

    void handleRoot() {
      Serial.print("The Host: header value: ");
      Serial.println(server.hostHeader());
      server.send(200, "text/plain", "hello from esp8266!\r\n");
    }
    

    Documentation for the ESP8266WebServer is here.