Recover one old project and get the problem connecting from arduino to my server using GET method.
Here is stripped function sendReq() which I believe was working few years before:
char NASServer[] = "192.168.0.101";
char NASServerPage[] = "/Arduino/Save_test.php"; //could be anything, not even existing file - the same error
void sendREQ() //client function to send/receie GET request data.
{
if (client.connect(NASServer, 80))
{
Serial.println(F("connected"));
client.print(F("GET "));
client.print(NASServerPage);
client.print(F("?name=value")); //simplified, real data is here
client.println(F(" HTTP/1.1")); //or HTTP/1.0 ???
client.println();
}
else
{
Serial.println(F("connection failed")); Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available())
{ //connected or data available
char c = client.read();
Serial.print(c);
}
Serial.println(F("disconnecting."));
client.stop();
}
Here is response:
connected
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 17 Jul 2019 03:39:21 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
disconnecting.
6v6gt:
What happens when you type 192.168.0.101/Arduino/Save_test.php?name=value in a web browser?
It returns correct expected response from web-server. I have simplified everything for troubleshooting so now it returns just OK. But it really does not related to my problem because of the problem is on header processing, before file processing. Even if i put there not existing file - it returns back the same error.
6v6gt:
Your GET request does not have a host parameter. Find an example similar to yours which is known to work. It is very sensitive to the format.
Actually, originally it was as " HTTP/1.0", with no header, and it was definitely working, but now give me that error. Let me look what exact response with " HTTP/1.0"... Ok, error is the same, but response is different, may be it will give some hint what is going on...
By some reason cannot insert response here. Put it on my server, link is here: link
Ok, seems like I have fixed, Host in header is required, even with "HTTP/1.0".
Probably, something has been changed on the server site, before Host was not needed.
Strange, with "HTTP/1.1" is not working, even with Host, but I will leave as it is, with 1.0.
Just last theoretical question - what is this "Host" field in header is for? What it will affect on?
Good working senREQ() function:
void sendREQ() //client function to send/receie GET request data.
{
if (client.connect(NASServer, 80))
{
Serial.println(F("connected"));
client.print(F("GET ")); client.print(NASServerPage); client.print(F("?name=value"));
client.println(F(" HTTP/1.0"));
client.println(F("Host: SomeHost"));
client.println();
}
else
{
Serial.println(F("connection failed")); Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available())
{ //connected or data available
char c = client.read();
Serial.print(c);
}
Serial.println(F("disconnecting."));
client.stop();
}