will.mendil
will.mendil

Reputation: 1035

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?

Upvotes: 0

Views: 1169

Answers (2)

NDB
NDB

Reputation: 618

The following code allows you to print both, host and URI to the console:

Serial.print("Request: ");
Serial.print(webServer.hostHeader());
Serial.println(webServer.uri());

Upvotes: 0

Juraj
Juraj

Reputation: 3736

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.

Upvotes: 0

Related Questions