Description

httpGET() / httpGETSSL()– function establishes a connection to a TCP listening server (e.g. a webserver), starts a http GET requests and returns the ‘Content-Length’ value reported by the server. The application can now read the actual data from the client object.

Syntax

myClient.httpGET( os_host, os_path, ou16_port );
myClient.httpGETSSL( os_host, os_path, ou16_port );

*note the SSL equivalent is not available on dice-ETH and the CAN2SER monitor in CANIP provider

Parameters

String os_host: a string specifying the host part of the URL (e.g. “di-ce.de”)
String os_path: a string of the path and file of the URL (e.g. “/index.html”)
optional: uint16_t ou16_port: TCP port of the request (=80 / 443 if omitted)

Returns

int:
0 – error no connection could be established
<0 – http error code of the webserver (e.g. -404 = file not found)
>0 – ‘Content-Length’ of the body. That is the number of bytes to be expected for the following read() calls.

Example

WiFiHTTPClient myClient;
myClient.httpGET( "di-ce.de", "/downloads/dice-ascii-logo.txt" );

Example Code

The following example can be used on a dice-WiFi because it makes use of the HTTP_Client type: WiFiHTTPClient. If you like to use the code on a different device, replace it by any of the following #includes & classes: CANIP_Client, EthernetHTTPClient or GSMHTTPClient

#include <WiFiNINA.h>
char ssid[] = "mySSID";
char pass[] = "myPass";
WiFiHTTPClient myClient;
void setup()
{
  while(!Serial);
  Serial.println("WiFiHTTPClient Example");
  while(WiFi.begin(ssid, pass) != WL_CONNECTED)
    delay(10000);
  Serial.println("Connected to WiFi");	
  int iResponse = myClient.httpGET("di-ce.de", 
                  "/downloads/dice-ascii-logo.txt");
  if( iResponse <= 0) 
    Serial.println("Error: "+String(iResponse));
  else
  {
    Serial.println("Connected to Server");
    while( myClient.available() || myClient.connected() )
    {
      while(myClient.available() > 0) 
      {
        if( Serial.availableForWrite())
	{
	  char c = myClient.read();
	  Serial.write(c);
	}
	yield();
      }
      yield();
    }
  }
  Serial.println("\n- disconnected from server -");
  myClient.stop();
}

void loop()
{ }

Notes and Warnings

Examples can be found in the Arduino IDE menu:
File -> Examples -> DICE -> HTTP_Client

See also

Find more details here:

HTTP_Client Class
writeFromClient()