Thingspeak publish via http / https

I have issue while trying to update a thingspeak channel using https
I works fine with http but not with https.

I extract the certificate from edge and store it as a resource ( api.thingspeak.com

I tried with a winform app and get both to work without any issue.
with tinyClr, I got one WebnException (Invalid operation)

Here is the code used to test
Any idea ?

try
{
// HTTP
var url = “http://api.thingspeak.com/update?api_key=MyKey&field2=12.6”;

if (HttpWebRequest.Create(url) is HttpWebRequest request)
{
    request.Method = "GET";

    using var response = request.GetResponse() as HttpWebResponse;
    if (response != null)
    {
        Debug.WriteLine(response is { StatusCode: HttpStatusCode.OK }
            ? "Data successfully sent to ThingSpeak (http)!"
            : $"Failed to send data: {response.StatusCode}");

    }
}

// HTTPS
url = "https://api.thingspeak.com/update?api_key=MyKey&field1=13.0";
// Load SSL certificate (root CA)
var certificate = Resources.GetBytes(Resources.BinaryResources.thingspeakCertificate);
var certx509 = new[] { new X509Certificate(certificate) };


request = HttpWebRequest.Create(url) as HttpWebRequest;
request.HttpsAuthentCerts = certx509; 
request.KeepAlive = false;
request.Method = "GET";
request.ReadWriteTimeout = 2000;

using var response2 = request.GetResponse() as HttpWebResponse;
if (response2 != null)
{
    Debug.WriteLine(response2.StatusCode == HttpStatusCode.OK
        ? "Data successfully sent to ThingSpeak (https)"
        : $"Failed to send data: {response2.StatusCode}");
}

}
catch (Exception e)
{
Debug.WriteLine(“Exception” + e.Message);
}

I don’t have any way to test your code, but I did look at the Thingspeak API documentation. It had this note: " Note that for embedded devices or clients that do not automatically URL encode GET requests, you must explicitly URL Encode query parameters before making the GET request from the device."

Could that me your issue?

Hi Steven

Thanks for your suggestion. But to my understanding URL encoding ensures that characters within query parameters are encoded in a way that they can be safely transmitted in a URL. the url seems to be the same for http and https. And the same Url works with the winform app for both Http and https.

Anyway, it seems that MQTT is more appropriate with devices like Feather … Data is sent quicker and with less power consumption.

I started with http because my first trial was been done with AWS.
I could connect with TinyClr but was not able to publish anything (while being to publish without any issue with a WinForm app)

So, I tried again to publish using MQTT on ThingSpeak.
The first trial was unsuccessful
Thingspeak use QoS Level 0, so even if the parameter is requested, the parameter Packet Id is normally not used.
If I am correct, the Packet ID should be between (1-65535) for QoS 1 and 2 but is not used for QoS 0.
It was to set to 0 and this generates one exception.
Changing the initial value to 1 solved the issue

In summary.
I will use https to download data via an app and use MQTT to publish from the Feather
ThingSpeak is to me a good alternative to AWS
Easier to setup and they have a free account as well