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?
Asked
Active
Viewed 645 times
0

will.mendil
- 752
- 2
- 6
- 21
-
1you don't know the hostname of your esp8266? the name was resolved by the client and the client sent the uri to the IP address of your esp8266 – Juraj Sep 11 '21 at 13:12
-
well, in the case of `server.onNotFound` I would like to know what the station was looking for. I can only see the uri. Isn't there a way to determine which full url was sent to the esp? – will.mendil Sep 11 '21 at 13:35
-
1you can read the Host header. 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 – Juraj Sep 11 '21 at 13:40
-
How do i read the Host header? Since everything is served by the esp, including dns, should I be able to get the http(s)://host:port part? – will.mendil Sep 11 '21 at 14:24
-
https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer#getting-information-about-request-headers – Juraj Sep 11 '21 at 14:29
-
@Juraj indeed `Serial.println(server.hostHeader());` solves my problem. You can answer the question if you want the badges – will.mendil Sep 11 '21 at 15:05
1 Answers
0
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.

Juraj
- 3,490
- 4
- 18
- 25