-1

I am aiming to make a post request to trigger a IFTTT webhook action. I am using the MKR1010 board. I am able to connect to the network and turn the connected LED on and off using the cloud integration.

The code is as follows, but doesn't trigger the web hook. I can manually paste the web address in a browser and this does trigger the web hook. When the code is posted it returns a 400 bad request error.

The key has been replaced in the below code with a dummy value.

Does anybody know why this is not triggering the web hook? / Can you explain why the post request is being rejected by the server? I don't even really need to read the response from the server as long as it is sent.

Thank you

// ArduinoHttpClient - Version: Latest 
#include <ArduinoHttpClient.h>


#include "thingProperties.h"


#define LED_PIN 13
#define BTN1 6

char serverAddress[] = "maker.ifttt.com";  // server address
int port = 443;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);


// variables will change:
int btnState = 0;         // variable for reading the pushbutton status
int btnPrevState = 0;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  // setup the board devices
  pinMode(LED_PIN, OUTPUT);
  pinMode(BTN1, INPUT);


}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  // read the state of the pushbutton value:

btnState = digitalRead(BTN1);
if (btnPrevState == 0 && btnState == 1) {
 led2 = !led2;
 postrequest();
}
  digitalWrite(LED_PIN, led2);

btnPrevState = btnState;

}



void onLed1Change() {
  // Do something

   digitalWrite(LED_PIN, led1);
    //Serial.print("The light is ");
    if (led1) {
        Serial.println("The light is ON");
    } else {
    //    Serial.println("OFF");
    }


}

void onLed2Change() {
  // Do something

  digitalWrite(LED_PIN, led2);
}


void postrequest() {
  //    String("POST /trigger/btn1press/with/key/mykeyhere")
 Serial.println("making POST request");
  String contentType = "/trigger/btn1press/with/key";
  String postData = "mykeyhere";

  client.post("/", contentType, postData);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);

    }
user2916488
  • 81
  • 1
  • 2
  • 15

1 Answers1

0

Why do you want to make a POST request and send the key in the POST body? The browser sends a GET request. It would be

client.get("/trigger/btn1press/with/key/mykeyhere");

In HttpClient post() the first parameter is 'path', the second parameter is contentType (for example "text/plain") and the third parameter is the body of the HTTP POST request.

So your post should look like

client.post("/trigger/btn1press/with/key/mykeyhere", contentType, postData);
Juraj
  • 3,490
  • 4
  • 18
  • 25
  • Hey, thanks for your response, If I understand what you are saying I should change the code to execute the GET request to fire the webhook rather than a POST (my misunderstanding of how this works!) If so I'll give that a go as IFTTT only formats the webhook string as you outline in the GET example not with the other two parameters. Or is it still doable with the POST example? – user2916488 Jun 05 '20 at 16:15
  • @user2916488, if it works as GET request from browser, then it should be a GET request. don't be confused by "get". GET is a request without body, POST is a request with body - content – Juraj Jun 05 '20 at 16:49
  • 1
    Thanks that change still gave a 400 error, but this time it was clear that the server port had to be 80 rather than 443. The combination of the two changes made it work and I can now fire multiple requests from different button presses - so can control Alexa from a button press :) – user2916488 Jun 05 '20 at 19:21
  • 1
    I think I've applied the bounty for this answer, but if not let me know! – user2916488 Jun 10 '20 at 16:10