I'm trying to send some data in NodeJS from one localhost server to another, but I'm getting a nasty error. On the server that receives the request it appears the request goes through because I'm getting an object logged to the terminal there, except its properties are null (they're supposed to be set to the properties in the req body, or null).
I get the error even if I try with Axios, which tells me that it’s not an issue with http.request or Axios. If anyone could help me understand what's going on I'd appreciate it.
Error:
events.js:377
throw er; // Unhandled 'error' event
^
Error: write EPROTO 8669511168:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:
at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:94:16)
Emitted 'error' event on ClientRequest instance
{
errno: -100,
code: 'EPROTO',
syscall: 'write'
}
The project sending the request looks like this:
Resources file
const http = require('http');
const options = {
host: 'localhost',
port: 3000,
path: '/create',
method: 'POST',
header: {
'Content-Type':'application/json'
}
}
const body = {
inquiryTotal: ‘20.00’
}
const stringifiedBody = JSON.stringify(body)
module.exports.Inquiries = {
create: () => {
req = http.request(options, (res) => {
console.log(res);
});
req.write(stringifiedBody); // this is an object that's already stringified to JSON
req.end();
/*axios.post('localhost:3000/v1/orders/create', {
orderTotal: '21.00'
}).then(res => {
console.log(res);
}).catch(err => {
console.log(err);
})*/
}
}
Server sending request:
const resource = require('./lib/resource');
// create server code
resource.Inquiries.create;
Assuming you are correctly setup your API endpoint, you can try this (added some comments inside the code):
const axios = require("axios").default;
const body = {
inquiryTotal: "20.00" // Use single-qupte ('') or double-quote ("") here and don't use back-quote (``)
};
module.exports.Inquiries = {
create: () => {
axios
.post("http://localhost:3000/v1/orders/create", body) // Add http:// in the beginning of your url
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
}
};