Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Humancell
#63457 I'm working on some code that is POSTing JSON to a user defined endpoint. I'm parsing the URI provided by the user, and can detect the scheme being specified: http vs. https

Now when I am wanting to POST the data, I am currently using WiFiClient ... but I want to now check the scheme (http vs. https) and then use the appropriate client.

My goal is that somehow at the beginning of my function I can chose the "client" to use by doing something like this:

Code: Select all    // http or https?
    if(!strcmp(endpointScheme, "http")) {
      // Use WiFiClient class to create TCP connections
      WiFiClient client;
      #ifdef DEBUG
      Serial.println(F("Using HTTP://"));
      #endif
    } else {
      // Use WiFiClientSecure class to create TCP connections
      WiFiClientSecure client;
      #ifdef DEBUG
      Serial.println(F("Using HTTPS://"));
      #endif
    }


If I could do this then I can simply use the "client" reference in the rest of my code, independent of it being a secure or non-secure connection. But this doesn't work. :-)

How can I do this?

Is there a way that I can define both in advance and then assign "client" to the one that I want? If so ... then what data type is "client"?

Or can I do this?

Code: Select all    // http or https?
    WiFiClient httpClient;
    WiFiClientSecure httpsClient;

    if(!strcmp(endpointScheme, "http")) {
      // Use WiFiClient class to create TCP connections
      client = httpClient;
      #ifdef DEBUG
      Serial.println(F("Using HTTP://"));
      #endif
    } else {
      // Use WiFiClientSecure class to create TCP connections
      client = httpsClient;
      #ifdef DEBUG
      Serial.println(F("Using HTTPS://"));
      #endif
    }


Is there some way to do this?