-1

The following code gets a server response of -1.

What am I doing wrong? I've done a bunch of research and this is the suggested code.

I've tested the server code with Postman, and it works fine, so it must be the other code.

Library used is ESP8266HTTPClient.h

ESP8266 code

void loop() { 
    HTTPClient http;

    http.begin("http://localhost:3003/record");
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    int httpCode = http.POST("Message");
    String payload = http.getString();

    Serial.println(httpCode);           // -1
    Serial.println(payload);            // nothing

    http.end();
}

Node Express server

var express = require("express");
var router = express.Router();

router.post("/record", function(req, res) {
    let message = req.body;
    console.log(message);

    res.status(200).send({
        message: message
    });
});

module.exports = router;

Also tried a different API, with a GET request. Still doesn't work.

void loop() { 
    HTTPClient http;

    http.begin("https://jsonplaceholder.typicode.com/posts/1");
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    int httpCode = http.GET();
    String payload = http.getString();

    Serial.println(httpCode);           // -1
    Serial.println(payload);            // nothing

    http.end();
}
Ivan
  • 1,967
  • 4
  • 34
  • 60
  • Trying a URL you know works is a great idea! Unfortunately, that example won't work because you're trying to access a secure URL (HTTPS) over an unencrypted connection. The Arduino SDK on the ESP8266 can do that but you'd have to write your code differently to make it work. Try an http: URL that you know works from a browser instead. – romkey Nov 12 '18 at 03:46

1 Answers1

0

Look at your URL:

http://localhost:3003/record

What does that mean?

localhost is shorthand for a special IP address - 127.0.0.1 - which means "this host"

You're not running the server you're trying to connect to on the ESP8266, so you can't use localhost there (the ESP8266 probably wouldn't resolve the name localhost anyway).

You need to replace localhost with the name or IP address of the server that your Node Express code is running on.

romkey
  • 6,218
  • 3
  • 16
  • 12
  • I am running the server locally on port `3003`. I get a good response in the browser and Postman. Are you saying localhost cannot be seen by the device? – Ivan Nov 11 '18 at 21:37
  • localhost can only be seen by the device that' it's referenced from. It means just that device - it means "self". Even if the ESP8266 understood localhost, localhost on it would refer to the ESP8266, not to the Node Express server - on the ESP8266, localhost would not cause it to communicate with any other computer. You must use either the the Node Express server's IP address or a real name - not localhost - for it on the ESP8266. You get a response from the browser and Postman because you're running them on the same computer as Node Express. – romkey Nov 12 '18 at 03:35