ESP8266/NodeMCU POST request returns -1 status code









up vote
-1
down vote

favorite












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










share|improve this question























  • 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.
    – John Romkey
    Nov 12 at 3:46














up vote
-1
down vote

favorite












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










share|improve this question























  • 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.
    – John Romkey
    Nov 12 at 3:46












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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










share|improve this question















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







http arduino esp8266 nodemcu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 21:47

























asked Nov 11 at 21:13









Ivan

380418




380418











  • 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.
    – John Romkey
    Nov 12 at 3:46
















  • 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.
    – John Romkey
    Nov 12 at 3:46















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.
– John Romkey
Nov 12 at 3:46




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.
– John Romkey
Nov 12 at 3:46












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer




















  • 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 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.
    – John Romkey
    Nov 12 at 3:35











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253295%2fesp8266-nodemcu-post-request-returns-1-status-code%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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.






share|improve this answer




















  • 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 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.
    – John Romkey
    Nov 12 at 3:35















up vote
0
down vote













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.






share|improve this answer




















  • 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 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.
    – John Romkey
    Nov 12 at 3:35













up vote
0
down vote










up vote
0
down vote









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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 21:29









John Romkey

64456




64456











  • 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 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.
    – John Romkey
    Nov 12 at 3:35

















  • 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 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.
    – John Romkey
    Nov 12 at 3:35
















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 at 21:37




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 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.
– John Romkey
Nov 12 at 3:35





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.
– John Romkey
Nov 12 at 3:35


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253295%2fesp8266-nodemcu-post-request-returns-1-status-code%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto