HTTP GET on MKR WIFI 1010

Hello everyone,
I am trying to send data on a webpage using GET method. Here's the code line I wrote:

    client.println("GET /?a=2 HTTP/1.1");
    client.println("Host: test1.webpage.it");
    client.println();
    client.println("Connection: close");
    client.println();

I don't see any update to the page. I am not sure about the two client.println(); commands.
Nevertheless, I checked the PING in working at that page through my board, the connection is successful ( client.connect(server, 80) ) and the server first line response is HTTP/1.1 200 OK.

Could you give me any suggestions?
Thank you very much

Welcome to the forum

You used code tags, congratulations. :slight_smile:

Often the problem is in the code you are not posting. Can you post a complete but short example that shows the issue?

Here the “almost full” code

void setup() {

   analogReadResolution(nbit);
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.print("Please upgrade the firmware. Your version is ");
    Serial.println(fv);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 8 seconds for connection:
    delay(8000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

 ping_server();   // 

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)==1) {
    Serial.print("connected to server: ");
    Serial.println(server);
    // Make a HTTP request:
    
    client.println("GET http://test1.webpage.it/?value=7 HTTP/1.1");
    client.println("Host: test1.webpage.it");
    client.println();
    client.println("Connection: keep-alive");
    client.println();
    //client.println("application/x-www-form-urlencoded");
    //client.println();
 
    Serial.println("\n-----------------------");
  }
  else  
  {
  Serial.println("\n NOT Connected to server");
  }

  

}

void loop() {
 
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();
    // do nothing forevermore:
    while (true);
  }
}

An "almost complete" code is not very useful because I cannot compile the sketch to look for compiler warnings and errors and I cannot run it on a board to see what happens.

Does not a HTTP request end with the first blank line? In your example that would be the blank line between the Host and Connection lines and therefore, afaik, the request is malformatted. You should still receive some response though...

In addition to posting the full code, post the output from the serial monitor as well.

JaBa:
Does not a HTTP request end with the first blank line? In your example that would be the blank line between the Host and Connection lines and therefore, afaik, the request is malformatted. You should still receive some response though...

In addition to posting the full code, post the output from the serial monitor as well.

Do you mean a blank line before or after the "GET" command?

BTW if I run the sample with this code, it works.

    client.println("GET /search?q=CIAO HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();

That's what I was getting at. The HTTP Request begins with the GET. Each text line after that gives some additional information. After all information is sent, the Request is terminated by sending an empty line which is exactly what client.println(); does. So, now that the request is properly formatted, the sample works.

JaBa:
That's what I was getting at. The HTTP Request begins with the GET. Each text line after that gives some additional information. After all information is sent, the Request is terminated by sending an empty line which is exactly what client.println(); does. So, now that the request is properly formatted, the sample works.

Unfortunately if I format my code as the example the value "a" is not acquired as well.
I am wondering the test1.webpage is not working properly while the code is correct

    client.println("GET /?a=2 HTTP/1.1");[color=#222222][/color]
    client.println("Host: test1.webpage.it");[color=#222222][/color]
    client.println("Connection: close");[color=#222222][/color]
    client.println();

I solve the issue. The code is correct but I must use SSL so

WiFiSSLClient client;

and

client.connect(server, 443)

Now the GET works and pass the parameters to the webpage

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.