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();
}