Open 1003657663 opened 1 year ago
Can you try using method: 'POST'
?
Can you try using
method: 'POST'
?
In fact, I use POST, and GET is a compatible solution that was changed later. I tried again. or not, this is the result
this is 8266 code complete
var s1 = new Serial();
s1.setup(9600, { tx: D12, rx: D13 });
var http = require('http');
var serverHost = '10.10.10.5';
var serverPort = '8888';
var serverPath = '/'
function wifiConnect() {
return new Promise((resolve, reject) => {
var wifi = require("Wifi");
wifi.connect('xxxxx', { password: 'xxxxxx' }, function (e) {
if (e) {
console.log('error during connect:', e);
wifi.disconnect();
reject();
} else {
console.log('connected to wifi success');
wifi.stopAP();
wifi.save();
resolve();
}
});
})
}
function sendDataToHomeServer(data, options) {
options = options || { method: 'GET' };
var options = {
host: serverHost, // host name
port: serverPort, // (optional) port, defaults to 80
path: serverPath, // path sent to server
method: 'POST', // HTTP command sent to server (must be uppercase 'GET', 'POST', etc)
protocol: 'http:', // optional protocol - https: or http:
headers: { 'Content-Type': 'application/json' } // (optional) HTTP headers
};
var req = http.request(options, function (res) {
res.on('data', function (data) {
console.log("HTTP> " + data);
});
res.on('close', function (data) {
console.log("Connection closed");
});
});
req.on('error', function (error) {
console.log('request error', error);
})
req.write('555555'); // this is not work
req.end('33333');
}
wifiConnect().then(() => {
setInterval(() => {
sendDataToHomeServer('openLight');
}, 2000);
})
// save();
the node coed is not change
Obviously the body of the POST request is not sent
Can you try using
method: 'POST'
?
The ineffectiveness has nothing to do with Content-Type: application/json, even if I comment out the header, the result is still the same. Node received the request from esp8266 but did not carry the body, and esp8266 also received the return value from node and printed it out correctly
Below is my esp8266 code
I created a simple node server to accept data
the req.write(data); is not work The node service received the request, but did not receive any data I try to pass in data from req.end(‘111’); it still doesn't work