Webhook problem with POST web form url encoded

Recently a web service I used to post data with a photon was changed from HTTP to HTTPS. Therefore I’m rewriting the code to post the data from the device via a webhook.

The basic concept I understand and I can even see that my webhook is triggered from the device when it should. However, I did not manage to get any response back from the webhook and consequently have no data to check if the form posting actually worked or failed.

Then handler for the does not seem to fire. The serial.print of the “data” part does not show up.

Detail question: Is it necessary to add a delay after triggering the webhook and asking for the reply in the code? If yes, how long? So far I added 1000ms. To have longer delays is actually not convenient since to makes the device looking unresponsive to the user.

See code snippets below:

void setup() {
    Particle.subscribe("hook-response/fertig", myHandler, "SHBtimer");

void loop() {
//some code to trigger myfunction

void myfunction(){
String dataString="{ \"schueler_id\": \"{{{"+user+"}}}\",  \"uebezeit_h\": \"{{{0}}}\", \"uebezeit_m\": \"{{{"+mycounterS+"}}}\", \"gesendet\": \"{{{Abschicken}}}\", \"\": \"\" }";
                Particle.publish("fertig", dataString, PRIVATE);

//handler to get reply from webhook and evaluate
  void myHandler(const char *event, const char *data) {
          String reply=data;
              if (reply.indexOf("200")>0) {
              Serial.println("found 200 at"+reply.indexOf("200"));
              } else {
              if (reply.indexOf("400")>0) {
              Serial.println("found 400 at"+reply.indexOf("200"));

The code I used to post to the website in HTTP directly form the device:

client.connect(LIB_DOMAIN, 80);
            client.println("POST /begabung/begabungposted.php HTTP/1.0");
            client.println("Host: " LIB_DOMAIN);
            client.println("Authorization: Basic myuserandpasswordAsbase64enoding="); 
            client.println("Content-Type: application/x-www-form-urlencoded");
            client.print("Content-Length: ");
            String mycounterS = String(mycounter);
            String commandstring = String("schueler_id="+user+"&uebezeit_h=0&uebezeit_m="+mycounterS+"&gesendet=Abschicken ");

I think this line may be causing problems:

Particle.subscribe("hook-response/fertig", myHandler, "SHBtimer");

It should be:

Particle.subscribe("hook-response/fertig", myHandler, MY_DEVICES);

@rickkas7 Thanks a lot, that brought me a step further. Now, I’m getting a reply sent to my photon!

Unfortunately the reply is not an acknowledgment from the webserver that the request succeed. What I get is de source code of the website and not the result of a post. Such as if I would do a curl to that website. What I’m doing wrong? The webhook should trigger the form to be submitted with the values parsed to the webhook from the photon.

Any ideas appreciated.

I haven’t followed your code completely through (consider reformatting it with proper indentation to make it easier to follow :wink: )
But I’m pretty sure you should not wrap your values in tripple handle bars {{{...}}} but like this (BTW, try staying away from String)

void myfunction() {
  char str[128];
  snprintf(str, sizeof(str), "{ \"schueler_id\": \"%s\", \"uebezeit_h\": \"0\", \"uebezeit_m\": \"%d\", \"gesendet\": \"Abschicken\", \"\": \"\" }", user, mycounterS);
  Particle.publish("fertig", str, PRIVATE);

And in your webhook definition you’d not refer to e.g {{{Abschicken}}} (the value) but rather {{gesendet}} (the key name) which will then be replaced by the cloud for it’s provided value (e.g. Abschicken).

In order to see whether or not the cloud has translated your published data into a correct request, you could try redirecting your webhook to hit a different endpoint which would just show you the raw request (previously requestb.in was such a well renowned service, but since has gone offline - I can’t vouche for its “successor” requestbin.net tho’).

I figured out that I directed the webhook to the wrong URL. I have to address the target php page of the request and not the URL of the form that talkes to this target php page. At least that way it seems to work.

@ScruffR could you elaborate why one should “try staying away from String”? I checked both methods (string and char) and both worked. But char was more tedious to deal with in the rest of the code.

I still a bit confused about what one gets back from the webhook request. Now I get the HTML code from the page a user gets displayed when successfully submitting the form. But I expected to see just some server generated html code that confirms that the POST request was successful ( eg. “OK 200”).

Because String uses dynamic memory allocation and over time that may cause heap fragmentation which can lead to hard to debug instabilities of the entire system.

It’s just a matter of practice :wink:

Has this server no endpoint that can hand you back the result in a more IoT friendly way?

I’d say that is not what the majority of webhook users would be satisfied with. When they request data they want back the data and not just a message meaning: “Yea, the data would be available, bye!”.