1

I'm trying to send a POST request to IFTTT from a Node MCU. Instead of writing the code from the scratch, I've tweaked an already existing code (for GET request). It does not seem to work, what am I doing wrong? Needless to say, I replaced the WIFI ssid, the password and the IFTTT token. When testing the POST request with CURL, it works perfectly. Here's how the request should look like:

https://maker.ifttt.com/trigger/{event_name}/with/key/my_super_secret_token

...and the code:

#include <ESP8266WiFi.h>

const char* ssid     = "mywifi";
const char* password = "mywifipassword";

const char* host = "maker.ifttt.com";

void setup() {
  Serial.begin(115200);
  delay(100);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "trigger/{trigga}/with/key/IFTTT_token";
  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

    // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}
5
  • Don't you need to send any actual data in the body of the POST request? Do you get any output on the serial console?
    – Gerben
    Mar 1, 2016 at 19:18
  • I get nothing on the serial console, not even error code. I don't think my request goes through at all.
    – alkopop79
    Mar 1, 2016 at 20:00
  • @Gerben, Not sure about sending data. IFTTT recipes do not expect data. That's what they say: Make a POST or GET web request to: maker.ifttt.com/trigger{event}/with/key/my_super_secret_token With an optional JSON body of: { "value1" : "", "value2" : "", "value3" : "" } The data is completely optional, and you can also pass value1, value2, and value3 as query parameters or form variables. This content will be passed on to the Action in your Recipe.
    – alkopop79
    Mar 1, 2016 at 20:03
  • I'm still not sure how to make this work but this one (with GET method) works nicely: bigjungle.net/blog/2015/6/26/connect-maker-to-anything-ifttt
    – alkopop79
    Mar 1, 2016 at 20:23
  • When you say you get nothing in the serial monitor, does that include the Serial.print()s in setup()? Nothing at all shows when you run the sketch? Perhaps its a baud rate issue? How is the ESP connected to the Node MCU? Mar 1, 2016 at 21:58

1 Answer 1

2

Problem I see is that the request is incomplete - the server will be waiting for your payload.

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" + 
           "Connection: close\r\n\r\n");

You haven't specified a Content-Length header so the payload should start after the headers finish - but you don't send anything.

You need to (at least):

  • add a payload or
  • add a Content-Length: 0 header or
  • add a payload with a matching Content-Length: header.

You can use curl to see a full set of valid POST headers like this:

curl -v -d "" http://localhost

You can use Chrome and Firefox's Web Inspector's Network tab to look at the raw headers of requests too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.