ESP8266 GET http request issue

Dear Arduino community,

I’m trying to connect my Arduino to a local webpage using ESP8266 WiFi shield. When I try the following snippet with example.com as a server and a host name or any other domain, it works. But when I try it with my own laptop IPv4 address I get an error.

This is the request snippet with example.com, the one that works:

const char destServer[] = "example.com";
const String htmlHeader = "HTTP/1.1 200 OK\r\n"
                          "Content-Type: text/html\r\n"
                          "Connection: close\r\n\r\n"
                          "<!DOCTYPE HTML>\r\n"
                          "<html>\r\n";
                          
const String httpRequest = "GET / HTTP/1.1\r\n"
                           "Host: example.com\n"
                           "Connection: close\n\n";

This is the same request with my IPv4 address, the one that does not work:

const char destServer[] = "192.168.1.105";
const String htmlHeader = "HTTP/1.1 200 OK\r\n"
                          "Content-Type: text/html\r\n"
                          "Connection: close\r\n\r\n"
                          "<!DOCTYPE HTML>\r\n"
                          "<html>\r\n";
                          
const String httpRequest = "GET / HTTP/1.1\r\n"
                           "Host: 192.168.1.105\n"
                           "Connection: close\n\n";

It generates the following error in the serial monitor (attached as a picture as well):

14:14:57.758 -> +IPD,0,531:HTTP/1.1 400 Bad Request
14:14:57.792 -> Date: Sat, 28 Mar 2020 11:14:57 GMT
14:14:57.826 -> Server: Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.2
14:14:57.893 -> Content-Length: 325
14:14:57.893 -> Connection: close
14:14:57.927 -> Content-Type: text/html; charset=iso-8859-1
14:14:57.963 -> 
14:14:57.963 -> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
14:14:58.029 -> <html><head>
14:14:58.029 -> <title>400 Bad Request</title>
14:14:58.062 -> </head><body>
14:14:58.097 -> <h1>Bad Request</h1>
14:14:58.097 -> <p>Your browser sent a request that this server could not understand.

14:14:58.199 -> </p>
14:14:58.199 -> <hr>
14:14:58.199 -> <address>Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.2 Server at localhost Port 80</address>
14:14:58.289 -> </body></html>
14:14:58.322 -> 0,CLOSED
14:14:58.358 -> 
14:14:58.358 -> Press any key to test server.

The page that I’m trying to run is hosted by XAMPP server, I can’t get access to 127.0.0.1 using Arduino as well, not sure if the issue is with the IP address itself or because of another reason.

p.s. When I try other IP addresses from the web, it works. It stops working with the local IP addresses only.

Let me know if you need any more information, thanks.

From the W3:
“If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value.”
Did you try an empty value?

"Host: \n"

aarg:
From the W3:
“If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value.”
Did you try an empty value?

"Host: \n"

Thanks @aarg… I tried keeping the host empty as shown below, but it didn’t work as well… I still get 400 Bad Request error

const char destServer[] = "192.168.1.105";
const String htmlHeader = "HTTP/1.1 200 OK\r\n"
                          "Content-Type: text/html\r\n"
                          "Connection: close\r\n\r\n"
                          "<!DOCTYPE HTML>\r\n"
                          "<html>\r\n";
                          
const String httpRequest = "GET / HTTP/1.1\r\n"
                           "Host: \n"
                           "Connection: close\n\n";

"When I try other IP addresses from the web, it works. It stops working with the local IP addresses only."

Well, to use the 127.0.0.1 address you have to have a web server running on that computer. To use the 192.168.1.105 address you have to have a web server operating on that network at that address. Do you have either of these?

zoomkat:
“When I try other IP addresses from the web, it works. It stops working with the local IP addresses only.”

Well, to use the 127.0.0.1 address you have to have a web server running on that computer. To use the 192.168.1.105 address you have to have a web server operating on that network at that address. Do you have either of these?

Hello @zoomkat,

Yes, I have apache server running from XAMPP stack…

You cannot use "\n", you must use "\r\n":

const String httpRequest = "GET / HTTP/1.1\r\n" //Good boy!
                           "Host: 192.168.1.105\n" //Oops
                           "Connection: close\n\n"; //Opps oops

Danois90:
You cannot use “\n”, you must use “\r\n”:

const String httpRequest = "GET / HTTP/1.1\r\n" //Good boy!

“Host: 192.168.1.105\n” //Oops
                          “Connection: close\n\n”; //Opps oops

Hello @Danois

I tried that as shown in the following snippet:

const String httpRequest = "GET / HTTP/1.1\r\n"
                           "Host: 192.168.1.105\r\n"
                           "Connection: close\r\n";

I get the following output, which is not correct, it is supposed to show the connection message with the url, but I’m getting empty lines…:

22:41:58.917 -> Press any key to connect client.
22:41:58.985 -> 
22:42:01.338 -> 
22:42:01.406 -> 
22:42:01.406 -> Press any key to test server.

A request is always ended with a double linebreak, you have converted "\n\n" to "\r\n", it should have been "\r\n\r\n"..

The server sending back the 400 bad request indicates the server is getting the request, but it has an issue with the request. I used Apache on my computer many years back. I don't see the actual request to the server, so you may need to check that your request is appropriate for the Apache file structure.

Danois90:
A request is always ended with a double linebreak, you have converted “\n\n” to “\r\n”, it should have been “\r\n\r\n”…

Thank you so much @Danois90

I implemented your solution and it worked, now I get an actual response instead of the 400 Bad Request error. For everyone’s reference, here’s the snippet after modification:

const char destServer[] = "192.168.1.105";
const String htmlHeader = "HTTP/1.1 200 OK\r\n"
                          "Content-Type: text/html\r\n"
                          "Connection: close\r\n\r\n"
                          "<!DOCTYPE HTML>\r\n"
                          "<html>\r\n";
                          
const String httpRequest = "GET / HTTP/1.1\r\n"
                           "Host: 192.168.1.105\r\n"
                           "Connection: close\r\n\r\n";

However, I’m not sure if it’s going to work with specific URI, apparently http requests are so sensitive for any minor change </3 . I hope that I won’t face any more troubles with getting the page setup.

Thanks everyone…