Esp32/arduino post request error

Hi,
I am trying to post some test data to emoncms, but no luck so far. Do any of you have working code for an esp32 or arduino post request? If yes can you share it, please? I am getting an “HTTP/1.1 302 Found” response.

here are the data from serial monitor and my code


WiFi connected
IP address: 
192.168.1.96
connecting to emoncms.org
Requesting URL: /input/post.json?node=2&private_key=exxxxxxxxxxxxxxxxxx1b&json={temperature:1}
HTTP/1.1 302 Found
Date: Wed, 20 Jun 2018 16:44:44 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.25
Set-Cookie: PHPSESSID=av4f2o44ega1orqhgta4ikt8k4; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: http://emoncms.org//user/login
Vary: Accept-Encoding
Content-Length: 0
Connection: close


#include <WiFi.h>

const char* ssid     = "xxxxxxxxxxx";
const char* password = "pxxxxxxxxxxxxxo";

const char* host = "emoncms.org";
const char* nodeId   = "2";
const char* privateKey = "e3abxxxxxxxxxxxxxxxxx6ff1b";

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

    //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);

    WiFiClient client;  
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
    }

    String json = "{temperature:";
    json += value;
    json += "}";

    String url = "/input/post.json?node=";
    url += nodeId;
    url += "&private_key=";
    url += privateKey;
    url += "&json=";
    url += json;

    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");
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 5000) {
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }

    // 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");
}

Without getting too deep into what you’re doing, I don’t have an ESP32 set up right now, but here are a couple of things you might check:

  1. You are using POST but you seem to be sending the data in a GET query string with no POST payload. I’d suggest getting it working with GET to start, which is simpler. The documentation can be a little confusing because they use /input/post in a GET transaction. The post in the URI refers to the Emoncms action of posting data to the input rather than the HTTP POST protocol.

  2. I’ve never user &private_key= and don’t see that in the API documentation. &apikey= works.

  3. While there are variants of these formats that are not necessarily in the documentation, I don’t see /input/post/json. Maybe try something from the documentation like this:

HTTP://emoncms.org/input/post?node=2&apikey=01234567890abcdef01234567890abcdef&json={temperature:1}

This is the “json like” format, which isn’t actually Json because temperature isn’t enclosed in quotes. You can go with real Json (I recommend ArduinoJson) but the keyword should be &fulljson= in that case.

  1. Lastly, if you use HTTPclient, you can move up a level and send the headers and data without bothering with the nitty gritty of HTTP.

Good Luck.