Arduino Yun and HTTP Post problem

I'm having some trouble with HTTP Post on the Arduino yun.

I am trying to tweet via thing speak, and it's not working.

This is the line of code I have

HttpClient client;
String httpBody = "api_key="+thingtweetAPIKey+"&status=it is alive!";
String httpDestination = "https://api.thingspeak.com/apps/thingtweet/1/statuses/update";
client.post(httpDestination, httpBody);

When I use http://www.hurl.it/ to send post it works, and the tweet appears, but the Yun is not having the same results. Nothing gets posted when the above code executes.

How would I troubleshoot this? I'm new to networking on a micro controllers and don't know what else to try :slight_smile:

Another small thing I noticed is that the post in client.post does not turn orange as it does in client.get or client.read. This is just a cosmetic bug, right?

http://starter-kit.nettigo.eu/2014/arduino-yun-sending-data-thingspeak-post/

Arduino YUN: sending POST requests

Shouldn't it be easier than that?

I see in the bridge library header file there is a function prototype for http post.

Arduino IDE :

HttpClient.cpp

Arduino IDE 1.5.6.-r2:

Has no HttpClient::post

Arduino IDE 1.5.8:

unsigned int HttpClient::post(String &url, String &data) {
    return post(url.c_str(), data.c_str());
}

unsigned int HttpClient::post(const char *url, const char *data) {
  begin("curl");
  if (insecure) {
    addParameter("-k");
  }
  addParameter("--request");
  addParameter("POST");
  addParameter("--data");
  addParameter(data);
  addHeader();
  addParameter(url);
  return run();
}

Upgrade IDE will support http post.

sonnyyu:
Arduino IDE :

http://arduino.cc/en/main/software#toc3

HttpClient.cpp

Arduino IDE 1.5.6.-r2:

Has no HttpClient::post

Arduino IDE 1.5.8:

Upgrade IDE will support http post.

I have the 1.5.8 IDE, and I used that http post function but it does not return the same results as hurl.it

Does the provided function work? I'm not getting anything when I use it.

HttpClient use curl, and curl need root CA install, to support HTTPS POST ( as well as GET) , The root CA is needed.

wget -O ca-certificates_20140325_ar71xx.ipk https://www.dropbox.com/s/rmvkunea1ifa9zn/ca-certificates_20140325_ar71xx.ipk?dl=0 --no-check-certificate
opkg update
opkg install openssl-util
opkg install ca-certificates_20140325_ar71xx.ipk

I compile ca-certificates for get rid of "--no-check-certificate" at wget.

You might change subject to "Arduino Yun and HTTPS Post/Get problem"

In your code you use

client.post(httpBody, httpDestination);

The method post wants Destination as first parameter and Body as second one. Try:

client.post( httpDestination, httpBody);

p.s: Looking at here ( API Reference - MATLAB & Simulink ) I see that the parameter for defining the key is called "key" instead of "api_key". I don't know ThinkSpeak so this is just an hint

@sonnyyu
It's just an HTTP post problem, HTTP get works great.
What is root CA? I don't understand what it is/why I need it.

@Angelo9999
Whoops, thanks for catching that. The order was reversed in the example I gave, it was right in the code I compiled though. api_key or key both work, but I tried just key in case and it still didn't work.

As @sonnyyu suggested, the problem is with SSL certificates. Yun supports SSL but can't verify certificates without prior installing additional packages.
The quick workaround is to call noCheckSSL() before calling post

Still not getting anything, this is my code now.

HttpClient client;
String httpBody = "api_key="+thingtweetAPIKey+"&status=it is alive!";
String httpDestination = "https://api.thingspeak.com/apps/thingtweet/1/statuses/update";
client.noCheckSSL();
client.post(httpDestination, httpBody);

UPDATE: Found the problem. I changed client.post to client.postAsynchronously and it worked.

No idea why, if someone could explain it would be much appreciated. :slight_smile:

The only difference is that the async version will not block sketch execution